记2017.09.04美图笔试

选择

Java基础嘛,继承,final,throw,catch包含的,我不熟的如下:

垃圾回收,考了很多,以前也看过一些,可能掌握的不够多,也没有多理解吧

设计模式,英文,天,我居然只看懂了两个,明天看看哈

Spring,这个...我很早就说要学了,还没开始,一是玩了,二是做笔试题,复习基础,耽误了,很短的时间,不知道自己秋招能准备到哪种程度,越高越好,加油加油。


编程:

第一题,俩int的二进制相同位数多少个,简单,

    Scanner cin = new Scanner(System.in);
    int a = cin.nextInt();
    int b = cin.nextInt();
    int c = a^b;  //不同的位数是1,然后求1的个数
    int temp=1;   //求每位的&1,求完一位左移一位,左移到多少呢,这个数为0的时候
    int n=0; //计数
    while(temp!=0){
    	if((c&temp)==temp)
    		n++;
    	temp=temp<<1;
    	
    }
    System.out.println(n);
  主要呢,是这个方法是我这三天刚看的《剑指offer》里某个题的小思想,刚好用上了,好开心,多看书果然有好处,很有动力继续看。

今天复习了查找、排序,顺便贴个图吧,但是代码没怎么打呀

哎呀哎呀,困了,你说,这个时间是总结好呢,还是继续学习好呢,我本来就是晚上效率更好,好了好了,总结完了赶紧睡觉

记2017.09.04美图笔试_第1张图片



第二题,来自牛客网

给出两个字符串,找出其中最长的公共连续子串,输出其长度。 //dp啊,我的各个笔试都有dp题,看来很重要,前天还搜了下背包九讲,感觉很有用。

import java.util.*;
public class Main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String s1 = sc.nextLine();
            String s2 = sc.nextLine();
            int l1 = s1.length();
            int l2 = s2.length();
            int max = 0;
            int[][] dp = new int[l1][l2];
            for (int i = 0; i < l1; i++){
                for (int j = 0; j < l2; j++){
                
					if (s1.charAt(i) == s2.charAt(j))
                        if (i == 0 || j == 0)
                            dp[i][j] = 1;
                        else
                            dp[i][j] = dp[i - 1][j - 1] + 1;                   
                    if (dp[i][j]>max)
                        max = dp[i][j];
                }
            }
            System.out.println(max);
        }
        sc.close();
    }
}

好了,笔试前还看了赛码网几道头条的题,有感,都是别人的代码哈,贴上来方便我自己看

 
  

头条的2017校招开始了!为了这次校招,我们组织了一个规模宏大的出题团队。每个出题人都出了一些有趣的题目,而我们现在想把这些题目组合成若干场考试出来。在选题之前,我们对题目进行了盲审,并定出了每道题的难度系数。一场考试包含3道开放性题目,假设他们的难度从小到大分别为a, b, c,我们希望这3道题能满足下列条件:

a<= b<= c
b - a<= 10
c - b<= 10

所有出题人一共出了n道开放性题目。现在我们想把这n道题分布到若干场考试中(1场或多场,每道题都必须使用且只能用一次),然而由于上述条件的限制,可能有一些考试没法凑够3道题,因此出题人就需要多出一些适当难度的题目来让每场考试都达到要求。然而我们出题已经出得很累了,你能计算出我们最少还需要再出几道题吗?

import java.util.Arrays;
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
	Scanner scan = new Scanner(System.in);
	int n = scan.nextInt();
	int[] arr = new int[n];
	for(int i=0;i20){
			res+=2;
			continue;
			//如果不到与20但是大于10,则还需要出一道题
		}else if(i+110){
			res+=1;
			i=i+1;
			continue;
			//如果在10以内的情况
		}else if(i+1
一个简单的办法,当时很急,没仔细看

import java.util.*;

public class Main {

	public static Scanner in = new Scanner(System.in);
	public static void main(String[] args) {
		
		System.out.println(solve());
	}
	
	public static int solve() {
		int n = in.nextInt();
		if (n == 0) {
			return 3;
		}
		int [] array = new int[n];
		for (int i = 0; i < n; i++) {
			array[i] = in.nextInt();
		}
		Arrays.sort(array);
		int res = 0;
		int tmp = 0;
		for (int i = 0; i < n; i++) {
			if (tmp == 0) {
				tmp = 2;
				continue;
			}
			if (array[i] - array[i - 1] <= 10) {
				tmp--;
			} else {
				res += tmp;
				tmp = 2;
			}
		}
		res += tmp;
		
		return res;
	}
	
}


给定整数m以及n个数字A1, A2, …, An,将数列A中所有元素两两异或,共能得到n(n-1)/2个结果。请求出这些结果中大于m的有多少个。

//哇,这个看到答案的时候,感觉自己很傻,这是二叉搜索树(好像是叫这个)的特点啊,人家居然想到了。

import java.util.Scanner;

class TrieNode{
	TrieNode[]next = new TrieNode[2];
	int count = 0;
	int digit = 0;
	public TrieNode(int digit){
		this.digit = digit;
	}
}

public class Main {
	public static void insert(TrieNode root, int number){
		TrieNode prev = root;
		for(int i = 31; i >= 0; i--){
			int digit = (number >> i) & 1;
			if(prev.next[digit] == null){
				prev.next[digit] = new TrieNode(digit);
			}
			prev = prev.next[digit];
			prev.count++;
		}
	}
	
  public static int query(TrieNode root, int a, int m, int k){
		if(root == null){
			return 0;
		}
		TrieNode prev = root;
		for(int i = k; i >= 0; i--){
			int mDigit = (m >> i) & 1;
			int aDigit = (a >> i) & 1;
			if(mDigit == 1 && aDigit == 1){
				if(prev.next[0] == null){
					return 0;
				} else{
					prev = prev.next[0];
				}
			} else if(mDigit == 1 && aDigit == 0){
				if(prev.next[1] == null){
					return 0;
				} else{
					prev = prev.next[1];
				}
			} else if(mDigit == 0 && aDigit == 0){
				return query(prev.next[0], a, m, i-1) + (prev.next[1] == null ? 0 : prev.next[1].count);
			} else if(mDigit == 0 && aDigit == 1){
				return query(prev.next[1], a, m, i-1) + (prev.next[0] == null ? 0 : prev.next[0].count);
			} 
		}
		return 0;
	}
  
	public static void main(String[] args) {
		TrieNode root = new TrieNode(-1);
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int m = sc.nextInt();
		int[]array = new int[n];
		for(int i = 0; i < n; i++){
			array[i] = sc.nextInt();
			insert(root, array[i]);
		}
		long result = 0;
		for(int number : array){
			result += query(root, number, m, 31);
		}
		System.out.println(result / 2);
	}
}

哎呀,困了,我28?号的搜狐,一道题差点就对了,耿耿于怀,记下错误

的避免方法,想一下超过边界的值测试用例嘛,

//唉~~我能想的出来,但是比较慢
		Scanner sc = new Scanner(System.in);
		int n=sc.nextInt();
		int m=sc.nextInt();
		
        int a[] = new int[m];
        int b[] = new int[n];
        for (int i = 0; i < m; i++) {
			a[i]=sc.nextInt();
			
		}
        b[0]=a[0];
      //b[i]个a[i%4];
        
        int i=0;
        //while (i=n-1){  //你写成了==,这样以后进来就不等于了但会大于,不应该是那个临界值,因为以后还会改变i的值
        			break;
        		}
        		b[i]=a[k%m];  //你写成了4,那是例子,个例
        		i++;
        		
			}
        }




这是我头条第二题代码,虽然不简洁,但是一开始我更傻,准备用数组存下每个最小min呢,根本不用嘛

 
   
  
Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();
		int x[]=new int[n];
		int max=0;
		int sum=0;
		int min=0;
		//Arrays.sort(x);
		
		for (int i = 0; i < n; i++) {
			System.out.println(x[i]);
		}
		for (int i = 0; i < n; i++) {
			x[i]=scan.nextInt();
		}
		for (int i = 0; i < n-1; i++) {
			max=max>(x[i]*x[i])?max:(x[i]*x[i]);
			sum=0;
			for (int j = i+1; j < n; j++) {
				if(x[j](sum*min)?max:(sum*min);
			}
		}
		System.out.println(max);


第一题坐标那个老超时,好气,下面是我的智障的,别人的我好像看了,忘了。

Scanner scan=new Scanner(System.in);
		int n=scan.nextInt();
		int x[]=new int[n];
		int y[]=new int[n];
		
		for (int i = 0; i < n; i++) {
			x[i]=scan.nextInt();
			y[i]=scan.nextInt();
		}
		
		for (int i = 0; i < n-1; i++) {
			
			for (int j = i+1; j < n; j++) {
				if(x[i]

今天还看了Java的存储,困,以后再讲吧。。明天好好学习。

你可能感兴趣的:(笔试,编程题)