1. 程式人生 > >360_2019年校園秋招筆試題(Java程式碼實現)

360_2019年校園秋招筆試題(Java程式碼實現)

時間:20180827      19:00~21:00

地點:遠端

崗位:Java開發工程師

分兩部分,第一部分選擇題,共40道,沒啥可說的,就是不明白為什麼還有C++的程式碼分析題(Java崗)。

第二部分程式設計題,一共三道。

一、算正方形面積。

大概題目:翻修城鎮,要求把所有的居民都包含到城鎮裡,請問城鎮最小的面積。(城鎮是正方形的,平行於座標軸)

輸入:第一行一個整數N,表示城鎮人口數;接下來N行,每一行是一個人的座標。(-1e9<x,y<1e9)
輸出:城鎮的最小面積是多少。

樣例輸入:
2
0 0
0 2

樣例輸出:
4

除錯程式,一直通不過所有測試用例。考完後看了下牛客網討論區,才知道忽略的一個重要問題,(-1e9<x,y<1e9),應該用long型或其他符合要求的型別。(網上有人說用BigInteger,沒有試驗過。)

上程式碼:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		int n = scanner.nextInt();
		long x = 0, y = 0;
		long x1 = 1000000000;
		long x2 = -x1;
		long y1 = x1;
		long y2 = x2;
		for (int i = 0; i < n; i++) {
			x = scanner.nextLong();
			y = scanner.nextLong();
			x1 = Math.min(x1, x);
			x2 = Math.max(x2, x);
			y1 = Math.min(y1, y);
			y2 = Math.max(y2, y);
		}
		long ans = Math.max(Math.abs(x1 - x2), Math.abs(y1 - y2));
		System.out.println(ans * ans);
		scanner.close();
	}
}

二、看花

輸入兩個數n,m;(1<=n<=2000,1<=m<=100);分別表示n次看花,m表示一共有m朵花兒。
接下來輸入n個數a[1]~a[n],a[i]表示第i次,小明看的花的種類;
輸入一個數Q(1<=Q<=1000000);表示小紅的問題數量。
輸入Q行 每行兩個數 l,r(1<=l<=r<=n); 表示小紅想知道在第l次到第r次,小明一共看了多少不同的花兒。

輸入樣例:
5 3
1 2 3 2 2
3
1 4
2 4
1 5

輸出樣例:
3
2
3

執行程式,超時,用的是list判斷是否是重複的花,應該用HashSet去重,應該就不會超時了吧。其實也想到了,但是hashset沒怎麼用過......還是得多練習啊!

上程式碼:

import java.util.HashSet;
import java.util.Scanner;

public class Flower {
	static final int MAX = 2005;
	static int flower[] = new int[MAX];
	static int n, m, q;
	static int[][] kinds = new int[MAX][MAX];
	 
	public static void getKindTable() {
	    HashSet set = new HashSet();
	    for (int i = 1; i <= n; i++) {
	        set.clear();
	        for (int j = i; j <= n; j++) {
	            set.add(flower[j]);
	            kinds[i][j] = set.size();
	        }
	    }
	}
	 
	public static int getAns(int l, int r) {
	    if (l > r) {
	        int t = l;
	        l = r;
	        r = t;
	    }
	    return kinds[l][r];
	}
	 
	public static void main(String[] args) {
	    Scanner sc = new Scanner(System.in);
	    n = sc.nextInt();
	    m = sc.nextInt();
	    for (int i = 1; i <= n; i++) {
	    	flower[i] = sc.nextInt();
	    }
	    getKindTable();
	    q = sc.nextInt();
	    for (int i = 0; i < q; i++) {
	        int l, r;
	        l = sc.nextInt();
	        r = sc.nextInt();
	        System.out.println(getAns(l, r));
	    }
	    sc.close();
	}
}

三、最長子序列

把其中一個序列逆序,求兩個序列的最長公共子序列。
上程式碼:

import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		int[] a = new int[N];
		int[] b = new int[N];
		for (int i = 0; i < N; i++) {
			a[i] = sc.nextInt();
		}
		for (int j = 0; j < N; j++) {
			b[j] = sc.nextInt();
		}
		System.out.println(maxLen(a, b));
		sc.close();
	}

	private static int maxLen(int[] a, int[] b) {
		String s1 = String.valueOf(a);
		String s2 = String.valueOf(b);
		s2 = new StringBuilder(s2).reverse().toString();
		int lcs = Lcs(s1, s2);
		return lcs;
	}

	private static int Lcs(String s1, String s2) {
		int m = s1.length();
		int n = s2.length();
		int[][] dp = new int[m + 1][n + 1];
		for (int i = 0; i <= m; i++) {
			dp[i][0] = 0;
		}
		for (int i = 0; i <= n; i++) {
			dp[0][i] = 0;
		}
		for (int i = 1; i <= m; i++) {
			for (int j = 1; j <= n; j++) {
				if (s1.charAt(i - 1) == s2.charAt(j - 1)) {
					dp[i][j] = dp[i - 1][j - 1] + 1;
				} else {
					dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1]);
				}
			}
		}
		return dp[m][n];
	}
}