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%
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章