秋招笔试

滴滴笔试 8.21

  • 60个选择,两个编程

第一题: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

  • 题目一 ac
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;
	}
}

  • 题目二:ac
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

  • 30个选择,2个编程

  • 题目一:爬楼梯,ac
    在这里插入图片描述

    package qianxin;
    
    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(climb(n));
    		}
    		
    		s.close();
    	}
    	
    	public static int climb(int n) {
          
          
    		if(n<1) return 0;
    		if(n<4) return n;
    		
    		int a = 2;
    		int b = 3;
    		for(int i=4; i<=n;i++) {
          
          
    			int res = a + b;
    			a = b;
    			b = res;
    		}
    		return b;
    	}
    }
    
    
  • 题目二:暴力,80%,应该不对( leetcode 135

    在这里插入图片描述

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。问有多少密码。
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_36281031/article/details/108131698