完美世界校招演算法題2017
阿新 • • 發佈:2019-02-03
7點開始筆試,遲到了半小時,幸好還是全做完了。一道比較,一道動態規劃,都挺有趣的,憑記憶描述題目,程式碼半小時寫出來的,未優化,用的Java
題目一
首先輸入一個整數n
有A、B兩個隊伍,每個隊伍有n個人,每個人都有一個整數型武力值,
A隊,B隊每個人相互比武,武力值大,隊伍+100,相等不加分,武力值小-100。每個人只出場一次。輸出A隊最高得分
輸入樣例
6
2 3 4 5 6 7
3 4 5 6 7 8
輸出
200
import java.util.Arrays;
import java.util.Scanner;
/**
* Created by 95112 on 11/1/2017.
*/
public class PK {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
long[] A = new long[n];
long[] B = new long[n];
for (int i =0 ; i< n ; i++)
A[i] = scanner.nextLong();
for (int i= 0 ; i <n ;i++)
B[i] = scanner.nextLong();
boolean[] Bused = new boolean[n];
boolean[] Aused = new boolean[n];
int sum = 0 ;
Arrays.sort(A);
Arrays.sort(B);
int count = 0;
for (int i = n-1 ; i>=0 ; i-- ){
for (int j = n-1 ; j>= 0 ; j--){
if (Bused[j])
continue;
if (A[i] == B[i])
break;
if (A[i] > B[j]){
Aused[i] = true;
sum += 100;
Bused[j] = true;
count++;
break;
}
}
}
for (int i = n -1 ; i>=0 ; i--) {
if (Aused[i])
continue;
for (int j = n-1; j >= 0; j--) {
if (Bused[j])
continue;
if (A[i] > B[j]){
Aused[i] = true;
sum += 100;
Bused[j] = true;
count++;
break;
}
if (A[i] == B[j]) {
Aused[i] = true;
Bused[j] = true;
count++;
break;
}
}
}
sum = sum - (n - count)*100;
System.out.println(sum);
}
}
/*
6
2 3 4 5 6 7
3 4 5 6 7 8
4
9 7 5 3
10 8 5 2
*/
題目二
小明解數學題,輸入n表示有n道題,然後第二行輸入每道題的分數,第三道題輸入每道題所花時間,第四行輸入 總共的考試時間。輸出最高分數。
import java.util.Scanner;
/**
* Created by 95112 on 11/1/2017.
*/
public class Math {
public static void main(String[] args)
{
Scanner scanner = new Scanner(System.in);
int amount = scanner.nextInt();
int[] scores = new int[amount];
int[] spentTime = new int[amount];
for (int i = 0 ; i< amount;i++)
scores[i] = scanner.nextInt();
for (int i =0 ; i < amount ; i++)
spentTime[i] = scanner.nextInt();
int Time = scanner.nextInt();
int[] dp = new int[Time+1];
for (int i = 0 ; i< amount; i++)
{
for (int j = Time ; j >=0 ; j--){
if ( j >= spentTime[i]){
dp[j] = max(dp[j] , dp[j - spentTime[i]] + scores[i]);
}
}
}
System.out.println(dp[Time]);
}
private static int max(int a, int b)
{
if (a > b)
return a;
else
return b;
}
}
/*
5
5 4 3 5 2
2 2 3 5 1
10
*/