2020校招笔试题

1. 华为实习笔试:

  1. n个糖果分给k个小朋友,共有几种分法,枚举每种分法。
  2. 顺时针旋转n阶方阵,输出旋转m次后的结果。
  3. 两段文字,第一段是在第二段上进行增加单词,删除单词,改变单词得来。每个增、删、改操作算一次校对,求最少校对次数。

2. vivo校招提前批笔试:

  1. 给两个数组A[ ]、B[ ],求在A中出现,B中没出现的元素。
  2. 给一个链表和两个位置m、n,对m到n之间的链表进行反转,输出新链表。
  3. 01揹包问题。

3. 华为秋招第一批笔试:

1.题目:小明是个强迫症卖家,有10000台设备,卖的均价要求最接近D元,输出卖出的台数N,总售价M输入 0<D<10,精确到小数点后12位 ; 输出 M N

public static void main(String[] args) {
 Scanner in = new Scanner(System.in);
        double d = in.nextDouble();
        double min = d;
        int N = 0;
        long M = 0;
        int nums = 1;
        while(nums <= 10000){
         long sum = Math.round(d*nums);//把一个数字舍入为最接近的整数。
         double single = Math.abs( (double)sum / nums - d);
         if(min > single){
          N = nums;
          M = sum;
          min = single;
         }
         nums++;
        }
        System.out.println(M + " " + N);
 }

2.判断一个旋转字符串是否可以包含另一个字符串。
旋转字符串举例:AABC -> BCAA
输入: AABC
ABCA
ABFSR
FS
UYT
HY
每次三组测试样例,奇数行为源字符串,偶数行为目标字符串,包含为1.
如上例输出为110

public static void main(String[] args) {
	Scanner in = new Scanner(System.in);
	String out = "";
	while (in.hasNext()) {
		String s1 = in.nextLine();
		String s2 = in.nextLine();
		String s = s1 + s1;
		if(s.indexOf(s2) != -1)
			out += 1;
		else
			out += 0;
	}
	System.out.println(out);
}

上代码,感觉很简单一题,但只通过了85%,求大神指教错误。数组越界。

4. 华为秋招笔试

1.设三角形的周长p=120,满足该周长、且每条边均为整数的直角三角形,有以下三个:
{20,48,52},{24,45,51},{30,40,50}。

现给定三角形的周长 p < 100000,求直角三角形个数。

思路:

先选定最短的一条边 0<a<=p/3,次短边 a<= b <120 - a - b,最长边 c = 120 - a - b。

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            int p = in.nextInt();
            System.out.println(fun1(p));
        }
    }

    private static int fun1(int p){
        int a,b,c;
        int s = 0;
        for(a = 1;a <= p/3;a++){
            for(b = a;b < p - a - b;b++){
                c = p - a - b;
                if(a*a + b*b == c*c){
                    s++;
                }
            }
        }
        return s;
    }
}

2.一个矩阵,5*5,取相邻(二个成员有一个边是相同的)的6个,输入一个6个成员列表,判断是否满足?

矩阵如下:
[[1,2,3,4,5],[11,12,13,14,15],[21,22,23,24,25],[31,32,33,34,35],[41,42,43,44,45]]

import java.util.*;

public class Main2 {
    public static int[][]  matrix = {{1,2,3,4,5},{11,12,13,14,15},{21,22,23,24,25},{31,32,33,34,35},{41,42,43,44,45}};
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            int[] array = new int[6];
            for(int i = 0;i < 6;i++){
                array[i] = in.nextInt();
            }
            System.out.println(fun2(array));
        }
    }

    private static int fun2(int[] array){
        int count = 0;
        for(int i = 0;i < array.length;i++){
            for(int j = 0;j < array.length;j++){
                if(i != j){
                    int temp = isNear(array[i],array[j]);
                    if(temp == 1){
                        count++;
                    }
                }
            }
        }
        if(count == 10){
            return 1;
        }
        return 0;
    }

    private static int isNear(int num1,int num2){
        int rows = 5;
        int cols = 5;
        int num1_Y = 0;
        int num1_X = 0;
        for(int i = 0;i < rows;i++){
            for(int j = 0;j < cols;j++){
                if(matrix[i][j] == num1){
                    num1_X = j;
                    num1_Y = i;
                }
            }
        }
        int[] rows_cols = {-1,1};
        for(int item : rows_cols){
            if(num1_Y + item < rows && num1_Y + item >= 0 && matrix[num1_Y+item][num1_X] == num2){
                return 1;
            }else if(num1_X+item < cols && num1_X+item >=0 && matrix[num1_Y][num1_X+item] == num2){
                return 1;
            }
        }
        return 0;
    }

}

使用并查集算法:

import java.util.Scanner;

public class Hawei_02 {
    private static int[][]  matrix = {{1,2,3,4,5},{11,12,13,14,15},{21,22,23,24,25},{31,32,33,34,35},{41,42,43,44,45}};
    private static int count = 6;//输入的六个元素中有几个连通分量
    private static int[] trees = new int[6];

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            int[] array = new int[6];
            for(int i = 0;i < 6;i++){
                array[i] = in.nextInt();
            }
            //将每个元素的初始根节点设为自己
            for(int i = 0;i < 6;i++){
                trees[i] = i;
            }
            //判断两两是否相邻,若相邻则并入同一个集合
            for(int i = 0;i < 6;i++){
                for(int j = i+1;j < 6;j++){
                    int x = Math.max(array[i],array[j]);
                    int y = Math.min(array[i],array[j]);
                    if(x - y == 1 || x - y == 10){
                        union(i,j);
                    }
                }
            }
            System.out.println(count == 1 ? 1 : 0);
            count = 6;
        }
    }

    private static int findRoot(int p){
        while (p != trees[p]){
            p = trees[p];
        }
        return p;
    }

    private static void union(int p,int q){
        int pRoot = findRoot(p);
        int qRoot = findRoot(q);
        if(pRoot == qRoot) return;
        trees[pRoot] = qRoot;
        count--;
    }
}

3.输入两个整型数组A和B,二者中的元素都满足唯一且无序,同时A中的元素在B中都存在,B中的元素在A中也存在,即A和B仅仅元素顺序可能不同,比如{1,3,5,2}和{3,2,1,5}。

现在想通过分别删除A和B的部分元素,使得A和B剩下的子序列完全相同。请输出数组A需要删除的最少元素数(注意数组B需要删除相同数量的元素)。

输入:

4
1 3 5 2
3 2 1 5

输出:

2

说明:

{1,3,5,2}和{3,2,1,5}的最长公共子序列有三个,分别是{1,5},{3,5},{3,2},所以至少需要删除两个元素。

解法一:求最长公共子序列

import java.util.*;

public class Main3 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            int num = in.nextInt();
            int[] a = new int[num];
            int[] b = new int[num];
            for(int i = 0;i < num;i++){
                a[i] = in.nextInt();
            }
            for(int j = 0;j < num;j++){
                b[j] = in.nextInt();
            }
            System.out.println(fun3(num,a,b));
        }
    }

    private static int fun3(int num,int a[],int b[]){
        int lengthA = a.length;
        int lengthB = b.length;
        int[][] dp = new int[lengthA+1][lengthB+1];
        for(int i = 1;i <= lengthA;i++){
            for(int j = 1;j <= lengthB;j++){
                if(a[i-1] == b[j-1]){
                    dp[i][j] = dp[i-1][j-1] + 1;
                }else {
                    dp[i][j] = Math.max(dp[i][j-1],dp[i-1][j]);
                }
            }
        }
        return num - dp[lengthA][lengthB];
    }


}

AC 50%,应该是内存超了,需要优化。

解法二:

用一个数组 pos[i] 记录 A[i] 在 数组 B 中出现的位置,然后求pos[i] 的最长上升子序列即可。

最长上升子序列的求法

package huawei;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class Huawei_03 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while(in.hasNext()) {
            int num = in.nextInt();
            int[] a = new int[num];
            int[] b = new int[num];
            for(int i = 0;i < num;i++){
                a[i] = in.nextInt();
            }
            for(int j = 0;j < num;j++){
                b[j] = in.nextInt();
            }
            System.out.println(num - fun3(num,a,b));
        }
    }

    private static int fun3(int num,int a[],int b[]){
        Map<Integer,Integer> map2 = new HashMap<>();
        //pos[i] = a[i]在b中的位置
        int[] pos = new int[num];
        for(int i = 0;i < num;i++){
            map2.put(b[i],i);
        }
        for(int i = 0;i < num;i++){
            pos[i] = map2.get(a[i]);
        }

        int dp[] = new int[num];
        dp[0] = 1;
        int maxans = 1;
        for(int i = 1;i < num;i++){
            int maxval = 0;
            for(int j = 0;j < i;j++){
                if(pos[j] < pos[i]){
                    maxval = Math.max(maxval,dp[j]);
                }
            }
            dp[i] = maxval + 1;
            maxans = Math.max(maxans,dp[i]);
        }
        return maxans;
    }

}

5. 浦发提前批

  1. 求字符串最长不重复子串,AC 0.25
    剑指offer和leetCode原题:https://leetcode-cn.com/problems/longest-substring-without-repeating-characters/solution/wu-zhong-fu-zi-fu-de-zui-chang-zi-chuan-by-leetcod/
  2. 猴子每天吃一些桃子的一半再加一个,吃了m天后桃子只剩一个,求桃子原来几个? AC 100%
  3. 大数相加求和,如“11111111111111111111111111111”+“2222222222222222222222”。AC 100%
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章