Zingfront筆試
1.⼩⽩喜歡在⿊板上拼數字,第⼀個時⾠寫了⼀個正整數X,然后每過⼀個時⾠會擦掉數字的最后⼀位,直到他全部擦⼲凈。整個過程中,⼩⽩會把數字記錄下來,然后算出總和sum。
例如:X = 680,出現的數字依次680,68,6,他們和為754,現在⼩⽩給出⼀個sum,輸出⼀個正整數x,能符合上述過程,否則輸出-1(例如sum=738)
public static int getNum(int sum){
/**
* 首先判斷sum的位數,根據位數計算xyz前面分別的倍數,倍數用數組裝
* 在檢驗這個sum是不是和算出的相等,相等則算出X,否則re -1
*/
int unit = 0;
unit = Integer.toString(sum).length();
// 數組中裝x,y,z...前面的倍數
int[] array = new int[unit];
array[0] = 1;
for (int i = 0; i < unit-1; i++) {
array[i+1] = array[i] + (int) Math.pow(10,i+1);
}
int checkNum = 0;
// 算出校驗checkNum與num比較
int[] num = new int[array.length];
for(int j = array.length-1;j >= 0;j--){
if(j == array.length-1){
num[j] = sum / array[j];
checkNum = checkNum + num[j] * array[j];
continue;
}
num[j] = (sum - checkNum) / array[j];
checkNum = checkNum + num[j] * array[j];
}
int X = 0;
// 不相等直接re -1
if(checkNum != sum){
return -1;
}
// 相等算出 X
for (int i = 0; i < num.length; i++) {
X = (int) (X + num[i] * Math.pow(10,i));
}
System.out.println("sum="+sum+" -> X="+X);
return X;
}
運行如下:

2.⻢路上有⼀塊⿊⽩斑⻢線,A代表⿊,B代表⽩,不過因為施⼯⼈員不⼩⼼,導致⽬前斑⻢線很奇怪,例如AAB,這個時候⼩⽩突發奇想,如果將AAB進⾏0次或者多次以下操作:
把斑⻢線從某個地⽅切開,然后得到的兩塊進⾏翻轉,在拼接在⼀起(AAB -> A , AB 旋轉拼接后ABA,得到最⻓連續⿊⽩相間的⻓度為3)求問,如果斑⻢線ABABAAABAB,他的最⻓連續⿊⽩相間⻓度為多少
public static int getMaxLength(String s) {
/**
* 由於題中說明:斑馬線顏色只有 黑A 白B,AAB可以進⾏0次或者多次切割操作
* 無疑只有兩種情況:AB、BA
* 所以直接以"A"切割字符串得到"A"的個數,"B"的個數也出來了
* 最終有兩種情況:1.AB相等數量時 2.AB數量不等時
*/
char[] chars = s.toCharArray();
int i = 0;
int j = 0;
for (char c : chars) {
if(c == 'A'){
i++;
}else {
j++;
}
}
if(i == j){
System.out.println("斑⻢線:"+s+" -> 最長長度:" +(i * 2));
return i * 2;
}else {
System.out.println("斑⻢線:"+s+" -> 最長長度:" +(Math.min(i,j) * 2 +1));
return Math.min(i,j) * 2 +1;
}
}
運行如下:

3.某俱樂部有4只⾜球隊A,B,C,D隊,現有1~20的級別划分每個隊伍,既⽬前每隊的戰⽃⼒⽔平級別在10,7,5,4的⽔平。現俱樂部為了優化各隊的能⼒⽔平,實施每⽉底從戰⽃⼒最⾼的隊伍抽出部分⼈⼒資源,均分配到其他⼩隊。(規則:分配3個戰⽃⼒的⼈⼒資源出來到其他⼩隊,所以經過⼀個⽉的調整分配,各⼩隊現在的戰⽃⼒⽔平:7、8、6、5)
那么請問,經過五年的戰⽃⼒優化調整后,哪個⼩隊的戰⽃⼒最⾼,為多少?編程求解該問題,並思考是否為最優解。
public static String getMaxForce(int[] s, int year) {
/**
* 因為總戰斗力不變 增加與減少也不變 到后面應該是有數學規律的
* 首先將對名、戰斗力轉成k-v存在map里
* 然后按照升序排列,得到list,再循環改變list的value
* 最后取list里最后一個元素的k-v
*/
Map<string, integer=""> map = new HashMap<>();
map.put("A",s[0]);
map.put("B",s[1]);
map.put("C",s[2]);
map.put("D",s[3]);
Map<string, integer=""> map1 = new HashMap<>();
for (int i = 0; i < year * 12; i++) {
if(i == 0){
List<map.entry<string, integer="">> list = getEntries(map);
list.get(3).setValue(list.get(3).getValue() - 3);
list.get(2).setValue(list.get(2).getValue() + 1);
list.get(1).setValue(list.get(1).getValue() + 1);
list.get(0).setValue(list.get(0).getValue() + 1);
map1 = getMap(list);
}
List<map.entry<string, integer="">> list = getEntries(map1);
list.get(3).setValue(list.get(3).getValue() - 3);
list.get(2).setValue(list.get(2).getValue() + 1);
list.get(1).setValue(list.get(1).getValue() + 1);
list.get(0).setValue(list.get(0).getValue() + 1);
map1 = getMap(list);
}
System.out.println("經過"+year+"年后,"+getEntries(map1).get(3).getKey()+"小隊戰斗力最高為"+getEntries(map1).get(3).getValue());
return getEntries(map1).get(3).getKey();
}
/**
* 升序排
* @param map
* @return
*/
private static List<map.entry<string, integer="">> getEntries(Map<string, integer=""> map) {
List<map.entry<string,integer>> list = new ArrayList<map.entry<string,integer>>(map.entrySet());
Collections.sort(list,new Comparator<map.entry<string,integer>>() {
public int compare(Map.Entry<string, integer=""> o1,
Map.Entry<string, integer=""> o2) {
return o1.getValue().compareTo(o2.getValue());
}
});
return list;
}
/**
* 轉map
* @param list
* @return
*/
public static Map<string, integer=""> getMap(List<map.entry<string, integer="">> list) {
return list.stream().collect(Collectors.toMap(Map.Entry<string, integer="">::getKey,Map.Entry<string, integer="">::getValue));
}
運行如下:

有兩組數,第⼀組數順序固定,請編程實現讓第⼆組數 相鄰數字間的⼤⼩關系和
第⼀組數相同,且第⼆組相鄰數字間的差值之和最⼤
下⾯給出⼀個示例
第⼀組數: 5 7 4 9
第⼆組數:1 2 3 4
第⼆組數排序結果:2 4 1 3
第⼆組數排序后的差值之和:7 = abs(2-4) + abs(4-1) + abs(1-3)
