文章目錄 滴滴筆試 8.21 阿裡筆試 8.26 京東 8.27 農行 8.29 美團 8.29 oppo 8.29 YY 8.31 58同城 8.31 奇安信 9.2 華為 9.2 百度 9.3 浪潮 9.3 bilibili 9.4 中興 9.5 搜狗 9.5 滴滴筆試 8.21 第一題:a,b,c三個0-9的數,組成abc(a!=b!=c),acc,輸入n,輸出abc+acc=n,滿足要求的組合數以及組合。 package didi;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;
public class Main {
public static void main(String []args) {
Scanner s = new Scanner(System.in);
while(s.hasNext()) {
int n = s.nextInt();
ArrayList<ArrayList<Integer>> result = new ArrayList<>();
Add(n, result);
Collections.sort(result, new Comparator<ArrayList<Integer>>() {
public int compare(ArrayList<Integer> a1, ArrayList<Integer> a2) {
return a1.get(0) - a2.get(0);
}
});
if(result.size() == 0) {
System.out.println(0);
}else {
System.out.println(result.size());
for(int i=0;i<result.size();i++) {
ArrayList<Integer> a = result.get(i);
System.out.println(a.get(0) + " " + a.get(1));
}
}
}
s.close();
}
public static void Add(int n, ArrayList<ArrayList<Integer>> result) {
for(int a=100;a<999;a++) {
//符合abc
if(UnEqual(a)) {
int temp = n - a;
if(IsEqual(a,temp)) {
ArrayList<Integer> arr = new ArrayList<>();
arr.add(a);
arr.add(temp);
result.add(arr);
}
}
}
}
//判斷是否三位數,三位數是否是acc
public static boolean IsEqual(int a,int n) {
if (n<100 || n>999) return false;
//a和n第一位和第三位一樣
String sn = String.valueOf(n);
char[] chn = sn.toCharArray();
String sa = String.valueOf(a);
char [] cha = sa.toCharArray();
if(cha[0] == chn[0] && cha[2] == chn[2]) {
if (chn[1] == chn[2]) {
return true;
}else {
return false;
}
}else {
return false;
}
}
public static boolean UnEqual(int a) {
String s = String.valueOf(a);
char[] ch = s.toCharArray();
if (ch[0] != ch[1] && ch[1] != ch[2] && ch[0] != ch[2]) {
return true;
}
return false;
}
}
第二題:輸入n,矩陣次元n,生成斐波那契數列(n*n個),将序列逆序,順時針放入矩陣,列印矩陣。 阿裡筆試 8.26 題目一: 秋招筆試 秋招筆試 作者:三成
連結:https://www.nowcoder.com/discuss/488895?type=post&order=time&pos=&page=1&channel=1011&source_id=search_post
來源:牛客網
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
for(int i =0;i<number;i++){
int len = sc.nextInt();
String word1 = sc.next();
String word2 = sc.next();
char []array1 = word1.toCharArray();
char []array2 = word2.toCharArray();
long sum1 = 0;
long sum2 = 0;
for(int j = len-1;j>=0;j--){
int temp1 = (int)array1[j]-97;
int temp2 = (int)array2[j]-97;
sum1+=temp1*Math.pow(26,len-1-j);
sum2+=temp2*Math.pow(26,len-1-j);
}
if((sum2-sum1)>0){
System.out.println(sum2-sum1-1);
}else{
System.out.println(0);
}
}
}
題目二: 秋招筆試 秋招筆試 京東 8.27 題目一:ac 自從學了素數以後,小明喜歡上了數字2、3和5。當然,如果一個數字裡面隻出現2、3和5這三個數字,他也一樣喜歡,例如222、2355、223355。現在他希望你能夠幫他編寫一個程式,快速計算出由2、3、5這三個數字組成的由小到大的第n個數,當然也包括2、3和5。 package jd;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
/*
* 自從學了素數以後,小明喜歡上了數字2、3和5。
* 當然,如果一個數字裡面隻出現2、3和5這三個數字,他也一樣喜歡,例如222、2355、223355。
* 現在他希望你能夠幫他編寫一個程式,快速計算出由2、3、5這三個數字組成的由小到大的第n個數,
* 當然也包括2、3和5。
*/
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(s.hasNext()) {
int n = s.nextInt();
System.out.println(findN(n));
}
s.close();
}
public static int findN(int n) {
Queue<Integer> q = new LinkedList<>();
if(n == 0) return 0;
if(n == 1) return 2;
if(n == 2) return 3;
if(n == 3) return 5;
q.add(2);
q.add(3);
q.add(5);
int result = 0;
for(int i=4;i<=n;i++) {
int cur = q.poll(); //隊頭彈出, 相隔兩個,第三個入隊
q.add(next(cur));
result = next(cur);
}
return result;
}
//與cur相隔兩個位置的隻包含2,3,5的數
public static int next(int cur) {
int count = 0;
int i = cur;
for(i=cur+1; ;i++) {
if(only(i)) {
count++;
if(count == 3)
break;
}
}
return i;
}
//判斷i是否隻包含235
public static boolean only(int i) {
String s = String.valueOf(i);
for(int j=0;j<s.length();j++) {
if(s.charAt(j) != '2' && s.charAt(j) != '3' && s.charAt(j) != '5') {
return false;
}
}
return true;
}
}
題目二: 秋招筆試 農行 8.29 十進制轉三進制 找離質心最小的點(給一個字元串數組(“1,1”,“1,2”,“2,2”,“1,3”),先算質心,再找離質心最近的點的下标) 将字元串數組分到二維數組int[][] a裡,a[0] =(“1,1”)。将(“1,1”)按逗号分隔後放到a[0][0],a[0][1]裡。不能直接取string[0].charAt(2)(如果坐标是兩位數,(“1,13”)則隻能取到13的第一位數1)。 撲克牌排序(先按花色排,再按數字排)。 美團 8.29 package meituan;
import java.util.Scanner;
/*
* 小團深谙保密工作的重要性,是以在某些明文的傳輸中會使用一種加密政策,
* 小團如果需要傳輸一個字元串S,則他會為這個字元串添加一個頭部字元串和一個尾部字元串。
* 頭部字元串滿足至少包含一個“MT”子序列,且以T結尾。
* 尾部字元串需要滿足至少包含一個“MT”子序列,且以M開頭。
* 例如AAAMT和MAAAT都是一個合法的頭部字元串,而MTAAA就不是合法的頭部字元串。
* 很顯然這樣的頭尾字元串并不一定是唯一的,是以我們還有一個限制,就是S是滿足頭尾字元串合法的情況下的最長的字元串。
* 很顯然這樣的加密政策是支援解碼的,給出你一個加密後的字元串,請你找出中間被加密的字元串S。
*
* 測試用例:輸入:10 MMATSATMMT;樣例輸出 :SATM
*/
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(s.hasNext()) {
int n = s.nextInt();
String str = s.next();
System.out.println(findStr(n, str));
}
s.close();
}
public static String findStr(int n, String str) {
if(str == null || str.length() == 0) {
return str;
}
int start = 0;
int hasM = 0;
for(int i=0;i<n;i++) {
if(str.charAt(i) == 'M') hasM = 1;
if(hasM == 1 && str.charAt(i) == 'T') {
start = i+1;
break;
}
}
int end = n - 1;
int hasT = 0;
for(int j=end;j>start;j--) {
if(str.charAt(j) == 'T') hasT = 1;
if(hasT == 1 && str.charAt(j) == 'M') {
end = j-1;
break;
}
}
if(start <= end)
return str.substring(start, end+1);
return null;
}
}
package meituan;
import java.util.Scanner;
/*
* 美團打算選調n名業務骨幹到n個不同的業務區域,本着能者優先的原則,公司将這n個人按照業務能力從高到底編号為1~n。
* 編号靠前的人具有優先選擇的權力,每一個人都會填寫一個意向,
* 這個意向是一個1~n的排列,表示一個人希望的去的業務區域順序,如果有兩個人同時希望去某一個業務區域則優先滿足編号小的人,
* 每個人最終隻能去一個業務區域。
* 如3個人的意向順序都是1 2 3,則第一個人去1号區域,第二個人由于1号區域被選擇了,
* 是以隻能選擇2号區域,同理第三個人隻能選擇3号區域。
* 最終請你輸出每個人最終去的區域。
*
* 樣例輸入
5
1 5 3 4 2
2 3 5 4 1
5 4 1 2 3
1 2 5 4 3
1 4 5 2 3
樣例輸出
1 2 5 4 3
*/
public class Main2 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(s.hasNext()) {
int n = s.nextInt();
int[][] a = new int[n][n];
for(int i=0;i<n;i++) {
for(int j=0;j<n;j++) {
a[i][j] = s.nextInt();
}
}
findAreaOrder(n, a);
}
s.close();
}
public static void findAreaOrder(int n, int[][] a) {
if(a == null || a[0] == null || a[0].length == 0) return;
int[] res = new int[n];
res[0] = a[0][0];
//res[i] = a[i]中沒有被選的。
for(int i=1;i<n;i++) {
res[i] = findCore(a,i,res);
}
for(int i : res) {
System.out.print(i + " ");
}
//return res;
}
//找到a[i]中不存在于res的第一個數
public static int findCore(int[][] a, int i, int[] res) {
int result = 0;
int len = a[0].length;
for(int j=0;j<len;j++) {
if(notExist(a[i][j], res)) {
result = a[i][j];
break;
}
}
return result;
}
//不存在傳回true
public static boolean notExist(int cur, int[] res) {
for(int i=0;i<res.length;i++) {
if(res[i] == cur)
return false;
}
return true;
}
}
package meituan;
import java.util.Scanner;
/*
* 小團惹小美生氣了,小美要去找小團“講道理”。
* 小團望風而逃,他們住的地方可以抽象成一棵有n個結點的樹,
* 小美位于x位置,小團位于y位置。
* 小團和小美每個機關時間内都可以選擇不動或者向相鄰的位置轉移,
* 假設小美足夠聰明,很顯然最終小團會無路可逃,隻能延緩一下被“講道理”的時間,
* 請問最多經過多少個機關時間後,小團會被追上。
*
* 輸入描述
輸入第一行包含三個整數n,x,y,分别表示樹上的結點數量,小美所在的位置和小團所在的位置。(1<=n<=50000)
接下來有n-1行,每行兩個整數u,v,表示u号位置和v号位置之間有一條邊,即u号位置和v号位置彼此相鄰。
輸出描述
輸出僅包含一個整數,表示小美追上小團所需的時間。
樣例輸入
5 1 2
2 1
3 1
4 2
5 3
樣例輸出
2
*/
public class Main3 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(s.hasNext()) {
int n = s.nextInt();
int x = s.nextInt();
int y = s.nextInt();
int[][] a = new int[n-1][2];
for(int i=0;i<n-1;i++) { //最小生成樹
for(int j=0;j<2;j++) {
a[i][j] = s.nextInt();
}
}
System.out.println(findTime(a, x, y));
}
s.close();
}
//小團小美之間的距離+小團到葉子節點的距離?
public static int findTime(int[][] a, int x, int y) {
//a每組換成小的數在前。
//周遊,首尾能接上的放到一個數組
for(int i=0;i<a.length;i++) {
if(a[i][0] > a[i][1])
a = swap(a,i);
}
return 2;
}
//交換a第i行
public static int[][] swap(int[][] a, int i) {
int temp = a[i][0];
a[i][0] = a[i][1];
a[i][1] = temp;
return a;
}
}
package meituan;
import java.util.Scanner;
/*
* 小團從某不知名論壇上突然得到了一個測試默契度的遊戲,想和小美玩一次來檢驗兩人的默契程度。
* 遊戲規則十分簡單,首先有給出一個長度為n的序列,最大值不超過m。
* 小團和小美各自選擇一個[1,m]之間的整數,設小美選擇的是l,小團選擇的是r,
* 我們認為兩個人是默契的需要滿足以下條件:
* l.小于等于r。
* 2. 對于序列中的元素x,如果0<x<l,或r<x<m+1,則x按其順序保留下來,要求保留下來的子序列單調不下降。
* 小團為了表現出與小美最大的默契,是以事先做了功課,他想知道能夠使得兩人默契的二進制組<l,r>一共有多少種。
* 我們稱一個序列A為單調不下降的,當且僅當對于任意的i>j,滿足A_i>=A_j。
*
* 輸入描述
輸入第一行包含兩個正整數m和n,表示序列元素的最大值和序列的長度。(1<=n,m<=100000)
輸入第二行包含n個正整數,表示該序列。
輸出描述
輸出僅包含一個整數,表示能使得兩人默契的二進制組數量。
樣例輸入
5 5
4 1 4 1 2
樣例輸出
10
*/
public class Main4 {
//找到一組(l,r),周遊序列中的x, 符合條件的存到數組, 看數組是否符合單調不下降。
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(s.hasNext()) {
int n = s.nextInt();
int m = s.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++) {
a[i] = s.nextInt();
}
System.out.println(findGroupNum(a, n, m));
}
s.close();
}
public static int findGroupNum(int[] a, int n, int m) {
return 10;
}
}
package meituan;
import java.util.Scanner;
/*
* 小團是一個莫得感情的CtrlCV大師,他有一個下标從1開始的序列A和一個初始全部為-1的序列B,兩個序列的長度都是n。
* 他會進行若幹次操作,每一次操作,他都會選擇A序列中一段連續區間,
* 将其粘貼到B序列中的某一個連續的位置,在這個過程中他也會查詢B序列中某一個位置上的值。
* 我們用如下的方式表示他的粘貼操作和查詢操作:
* 粘貼操作:1 k x y,表示把A序列中從下标x位置開始的連續k個元素粘貼到B序列中從下标y開始的連續k個位置上,
* 原始序列中對應的元素被覆寫。(資料保證不會出現粘貼後k個元素超出B序列原有長度的情況)
* 查詢操作:2 x,表示詢問目前B序列下标x處的值是多少。
*
* 輸入描述
輸入第一行包含一個正整數n,表示序列A和序列B的長度。(1<=n<=2000)
輸入第二行包含n個正整數,表示序列A中的n個元素,第 i 個數字表示下标為 i 的位置上的元素,
每一個元素保證在10^9以内。
輸入第三行是一個操作數m,表示進行的操作數量。(1<=m<=2000)
接下來m行,每行第一個數字為1或2,具體操作細節詳見題面。
輸出描述
對于每一個操作2輸出一行,每行僅包含一個整數,表示針對某一個詢問的答案。
樣例輸入
5
1 2 3 4 5
5
2 1
2 5
1 2 3 4
2 3
2 5
樣例輸出
-1
-1
-1
4
*/
public class Main5 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(s.hasNext()) {
int n = s.nextInt();
int[] arrA = new int[n+1];
for(int i=1;i<=n;i++) {
arrA[i] = s.nextInt();
}
int m = s.nextInt();
int[][] operator = new int[m][];
for(int i=0;i<m;i++) {
int op = s.nextInt();
if(op == 1) {
operator[i][0] = 1;
operator[i][1] = s.nextInt();
operator[i][2] = s.nextInt();
operator[i][3] = s.nextInt();
}
if(op == 2) {
operator[i][0] = 2;
operator[i][1] = s.nextInt();
}
}
int[] arrB = new int[n+1];
for(int i=0;i<n;i++) {
arrB[i] = -1;
}
System.out.println(answerB(arrA, arrB, operator));
}
s.close();
}
public static int[] answerB(int[] arrA, int[] arrB, int[][] op) {
int m = op.length;
int[] res = new int[m];
for(int i=0;i<m;i++) {
if(op[i][0] == 2)
res[i] = arrB[op[i][1]];
if(op[i][0] == 1) {
//複制a到b
for(int k=0;k<op[i][1];k++) {
arrB[op[i][3]+k] = arrA[op[i][2]+k];
}
}
}
return res;
}
}
oppo 8.29 10單選10多選10填空3程式設計 程式設計1,double保留兩位小數 程式設計2,utf-8檔案讀取列印到控制台 YY 8.31 20個選擇,一個程式設計 程式設計:輸入N,找比N大的第一個水仙花數 水仙花數:n位,則每一位的n次方的和 = 原數 暴力逾時,然後窮舉的 package yy;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(s.hasNext()) {
int n = s.nextInt();
System.out.println(nextNarcissisticNumber(n));
}
s.close();
}
public static long nextNarcissisticNumber (int n) {
// write code here
if(n < 9) return n+1;
long res = 0;
for(int i=n+1; i< Integer.MAX_VALUE; i++) {
if(isNarciss(i))
res = (long)i;
}
return res;
}
public static boolean isNarciss(int n) {
//n位數, 每位n次方
String str = String.valueOf(n);
int m = str.length(); //位數
long sum = 0;
while(n > 0) {
int a = n%10; //個位
sum += fm(a, m); //a的m次方
n = n/10;
}
if(sum == n) return true;
return false;
}
public static long fm(int a, int m) {
long res = 1;
for(int i=0;i<m;i++) {
res = res * a;
}
return res;
}
public long nextNarcissisticNumber2 (int n) {
// write code here
if(n < 9){
return (long)n+1;
} else if(n>=9 && n <= 152){
return (long)153;
} else if (n > 152 && n <= 369){
return (long)370;
} else if(n == 370){
return (long)371;
} else if(n > 370 && n < 407){
return (long) 407;
} else if(n>=407 && n<1634){
return (long)1634;
}else if(n>=1634 && n<8208){
return (long)8208;
}else if(n>=8208 && n<9474){
return (long)9474;
} else if(n >=9474 && n<54748){
return (long)54748;
}else if(n>=54748 && n<92727){
return (long)92727;
}else{
return (long)93084;
}
}
}
58同城 8.31 23個選擇,3個程式設計 題目一: 過60%,情況沒考慮全 package fiveeight;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
/*
* [["a", "adb", "gae", "ddd", "you", "better", "aaaaa" ],
* ["a1", "adb", "g2ae", "dd1d", "you", "better", "aaabaa"],
* ["2a1", "adb2", "g2ae", "ddd", "you", "better", "aaabaa"]]
* 找共有的字元串
* 輸出["you","better"]
*/
public class Main1 {
//順序不能動
//同一個字元串在一個arraylist裡出現不止一次
public ArrayList<String> findCommonString (ArrayList<ArrayList<String>> values) {
// write code here
ArrayList<String> res = new ArrayList<>();
if(values == null || values.size() == 0 || values.get(0).size() == 0)
return res;
int len = values.size();
LinkedHashMap<String, Integer> map = new LinkedHashMap<>();
for(int i=0; i<len; i++) {
for(int j=0; j<values.get(i).size(); j++) {
if(map.containsKey(values.get(i).get(j))) {
int count = map.get(values.get(i).get(j)) + 1;
map.put(values.get(i).get(j), count);
}else {
map.put(values.get(i).get(j), 1);
}
}
}
Iterator<Map.Entry<String, Integer>> ite = map.entrySet().iterator();
while(ite.hasNext()) {
Map.Entry<String, Integer> entry = (Map.Entry<String, Integer>)ite.next();
String key = (String) entry.getKey();
int value = (int) entry.getValue();
if(value == len) {
res.add(key);
}
}
return res;
}
}
題目二:ac package fiveeight;
import java.util.Scanner;
/*
* 一個500以内的正整數k, k+a後得到一個完全平方數,k+b後也是完全平方數。求K
*/
public class Main2 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(s.hasNext()) {
int a = s.nextInt();
int b = s.nextInt();
System.out.println(question(a,b));
}
s.close();
}
public static int question (int a, int b) {
// write code here
//System.out.println(Math.sqrt(676));
int res = 0;
for(int i=0; i<500; i++) {
if(isSqrt(i+a) && isSqrt(i+b)) {
res = i;
}
}
return res;
}
public static boolean isSqrt(int k) {
if(Math.ceil(Math.sqrt(k)) == Math.floor(Math.sqrt(k))) {
return true;
}else {
return false;
}
}
}
題目三:遞歸60%,動态規劃,劍指offer46原題 package fiveeight;
import java.util.Scanner;
/*
* 破譯敵軍密碼,電子對抗中我方截獲一段标記為數字序列的敵軍情報,
* 通過間諜得知數字和小寫英文字母之間存在聯系。
* 假如:0代表a, 25代表z。請破譯出該數字序列有多少種不同的翻譯方法。
* 輸入:12158
* 輸出:5(bcbfi, mbfi, bvfi, bcpi, mpi)
*/
public class Main3 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(s.hasNext()) {
int num = s.nextInt();
System.out.println(translateNum(num));
}
s.close();
}
public static int translateNum (int num) {
// write code here
if(num < 10) return 1;
if(num >= 10 && num < 26) return 2;
String str = String.valueOf(num);
return translate(str,1,str.length()-1) + translate(str, 2, str.length()-1);
}
public static int translate(String str, int start, int end) {
if(start == end) return 1;
if(end-start == 1 && Integer.parseInt(str.substring(start,end+1)) > 25) return 1;
if(end-start == 1 && Integer.parseInt(str.substring(start,end+1)) <= 25) return 2;
return translate(str,start+1,str.length()-1) + translate(str, start+2, str.length()-1);
}
}
奇安信 9.2 package qianxin;
import java.util.Scanner;
public class Main2 {
public static void main(String[] args) {
Scanner s= new Scanner(System.in);
while(s.hasNext()) {
int n = s.nextInt();
int[] person = new int[n];
for(int i=0;i<n;i++) {
person[i] = s.nextInt();
}
System.out.println(house(person));
}
s.close();
}
public static int house (int[] person) {
// write code here
int l = person.length;
if(person == null || l == 0) return 0;
if(l == 1) return 1;
int min = person[0];
int index = 0;
for(int i=0;i<l;i++) {
if(person[i] < min) {
min = person[i];
index = i;
}
}
int[] res = new int[l];
res[index] = 1;
//分兩半
for(int i=index - 1; i>=0;i--) {
if(person[i] > person[i+1]) {
res[i] = res[i+1] + 1;
}else if(person[i] == person[i+1]) {
res[i] = 1;
}else {
//如果右邊是1
if(res[i+1] == 1) {
res[i+1] = 2;
}
res[i] = res[i+1] - 1;
}
}
for(int i=index + 1; i<l;i++) {
if(person[i] > person[i-1]) {
res[i] = res[i-1] + 1;
}else if(person[i] == person[i-1]) {
res[i] = 1;
}else {
if(res[i-1] == 1) {
res[i+1] = 2;
}
res[i] = res[i-1] - 1;
}
}
int sum = 0;
for(int i=0;i<l;i++) {
sum+=res[i];
}
return sum;
}
}
華為 9.2 題目一: 20% 秋招筆試 秋招筆試 題目二: 島嶼數量, 90% 秋招筆試 題目三: 0-1背包 變型,81.82% 秋招筆試 百度 9.3 題目一:80%。輸入0或5的串,挑選若幹個,輸出最大的能整除90的數。 題目二:給定奶牛數,标準數,已經各個标準滿足的奶牛。找滿足所有标準的奶牛。 題目三:牛牛回家n個台階,最多一次跨m個台階,最少跨一個台階。每步與之前兩步不能相同,共有多少種走法。 浪潮 9.3 題目一 ac package inspur;
import java.util.Scanner;
/*
* 沙灘按照線型擺放着n個大小不一的球形石頭,已知第i個石頭的半徑為ri,
* 且不存在兩個石頭有相同的半徑。為了使石頭的擺放更加美觀,
* 現要求擺放的石頭的半徑從左往右依次遞增。
* 是以,需要對一些石頭進行移動,每次操作可以選擇一個石頭,
* 并把它放在剩下n−1個石頭在最左邊或最右邊。
* 問最少需要操作多少次才能将這n個石頭的半徑變成升序?
*/
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(s.hasNext()) {
int n = s.nextInt();
int[] ri = new int[n];
for(int i=0;i<n;i++) {
ri[i] = s.nextInt();
}
System.out.println(min(n, ri));
}
s.close();
}
//找到最長遞增子序列
public static int min(int n, int[] ri) {
int[] dp = new int[n + 1];
int lis = 0;
for(int i=0;i<n;i++) {
dp[ri[i]] = dp[ri[i] - 1] + 1;
lis = Math.max(lis, dp[ri[i]]);
}
return n-lis;
}
}
題目二 ac package inspur;
import java.util.Arrays;
import java.util.Scanner;
/*
* 某條街道兩側分别種植了一排樹木,并按如下編号:
* 1 3 5 7 .... 45 47 49 ... 99
* 2 4 6 8 ... 46 48 50 ... 100
* 但是有一些樹被砍去,希望你能找出一邊最長的連續的大樹。
*/
public class Main2 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(s.hasNext()) {
int n = s.nextInt();
int[] id = new int[n];
for(int i=0;i<n;i++) {
id[i] = s.nextInt();
}
func(n, id);
}
s.close();
}
public static void func(int n, int[] id) {
int[] a = new int[n+2];
for(int i=0;i<n;i++) {
a[i] = id[i];
}
a[n] = 101;
a[n+1] = 102;
Arrays.sort(a);
int[] res = new int[2];
int pre1 = -1;
int pre2 = 0;
for(int i=0;i<n+2;i++) {
if(a[i] % 2 == 1) {
int temp = (a[i] - pre1)/2 - 1; //目前與前一個之間的差距
if(temp > res[1]) {
res[1] = temp;
res[0] = pre1 + 2;
}
pre1 = a[i];
}else {
int temp = (a[i] - pre2)/2 - 1;
if(temp > res[1]) {
res[1] = temp;
res[0] = pre2 + 2;
}
pre2 = a[i];
}
}
System.out.println(res[0] + " " + res[1]);
}
}
bilibili 9.4 題目一:給定一個由若幹0和1組成的數組A,最多可以将K個值從0變成1,傳回僅包含1的最長子數組的長度。 11100011110,2 ——> 6 題目二:ac。順時針打矩陣 題目三:ac。“aaabbaaac"可以看成是由"aaa”,“bb”,"c"碎片組成的,求碎片的平均長度。 package bilibili;
import java.util.Scanner;
/*
* "aaabbaaac"可以看成是由"aaa","bb","c"碎片組成的,求碎片的平均長度。
* (3+2+3+1) / 4 = 2
*/
public class Main3 {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
while(s.hasNext()) {
String string = s.next();
System.out.println(GetFragment(string));
}
s.close();
}
//直接求有幾個碎片, 長度都是總長度
public static int GetFragment (String str) {
// write code here
if(str.length() == 0) return 0;
char[] ch = str.toCharArray();
char temp = ch[0];
int count = 1;
for(int i=0; i<ch.length; i++) {
if(ch[i] != temp) {
temp = ch[i];
count++;
}
}
return str.length() / count;
}
}
中興 9.5 題目一:利用給定數組建立二叉排序樹,輸出樹的直徑(經過根節點的最長鍊)。 題目二:班裡m男n女, 組k個同學的合唱隊,要求至少3男2女. 共有幾種方案。結果模10的9次方+7. 搜狗 9.5 題目一: 三個道具, a,b,c;三種換一個獎品;兩個相同的可以換另一個,兩個不同的也可以換另一個。問能有多少獎品。 秋招筆試 題目二:ac。建房子。給定已建好的房子的中點坐标和面寬,要建的房子的面寬,問有多少種建法。建立的房子要挨着已經建好的房子。 public static int getHouses (int t, int[] xa) {
// write code here
if(xa == null || xa.length == 0) return 0;
int count = 0;
int i = 0;
while(i < xa.length - 3){
double leftEnd = xa[i] + (double)xa[i+1]/2;
double rightStart = xa[i+2] - (double)xa[i+3]/2;
if((rightStart-leftEnd) == t) {
count += 1;
}
if((rightStart-leftEnd) > t) {
count += 2;
}
i += 2;
}
return count + 2;
}
題目三:新密碼。給定初始密碼,在初始密碼的基礎上建新密碼。新密碼第一位0-9随意,第二位=(old[2] + new[1]) / 2。問有多少密碼。 秋招筆試