Zingfront智线
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)

浙公网安备 33010602011771号