微众银行笔试
第一题
思路:哈希解法
public static void main(String[] args) {
//1.不同段有不同的口味
//2.选尽可能长的糖果
//3.最多能买多长的糖果(价格最优,口味不能重复)
Scanner sc = new Scanner(System.in);
int arrayLength = sc.nextInt();
int i = 0;
int [] array = new int[arrayLength];
while (i<arrayLength){
array[i] = sc.nextInt();
i++;
}
int maxLength=0;
Map<Integer,Boolean> map = new HashMap<>();
for (int j = 0; j < array.length; j++) {
if (!map.containsKey(array[j])) {
map.put(array[j],true);
maxLength++;
}else {
break;
}
}
System.out.println(maxLength);
}
第二题
思路:同样是哈希解法去重即可
public static void main(String[] args) {
//1.通过给所有橡皮泥士兵加单位橡皮泥从而实现不同橡皮泥不同的风格
//2.至少需要加多少单位橡皮泥才能实现这个目标
Scanner sc = new Scanner(System.in);
int arrayLength = sc.nextInt();
int i = 0;
int [] array = new int[arrayLength];
while (i<arrayLength){
array[i] = sc.nextInt();
i++;
}
//通过Map获取重复大小的橡皮泥,重复大小的橡皮泥就是要变化大小的橡皮泥
int mixNum=0;
List<Integer> list = new ArrayList<>();
Map<Integer,Boolean> map = new HashMap<>();
for (int j = 0; j < array.length; j++) {
if (map.containsKey(array[j])){
list.add(array[j]);
}else {
map.put(array[j],true);
}
}
for (int k = 0; k < list.size(); k++) {
Integer item = list.get(k);
while (map.containsKey(item)){
item = item+1;
mixNum++;
}
//已经累加到不重复的橡皮泥要加入到map集合中
map.put(item,true);
}
System.out.println(mixNum);
}
第三题
思路:正常模拟获取所有子区间然后判断
但是我明明获取了所有子区间,为什么AC只有18%,求大佬救救
public static void main(String[] args) {
//1.一轮循环获取数组的子区间,子区间的长度为1开始,逐渐累加到数组的长度
Scanner sc = new Scanner(System.in);
int arrayLength = sc.nextInt();
double u = sc.nextDouble();
double v = sc.nextDouble();
double result = u/v;
int i = 0;
int [] array = new int[arrayLength];
while (i<arrayLength){
array[i] = sc.nextInt();
i++;
}
double numLength=1;
double sum=0;
int record=0;
for (int j = 0; j < array.length && numLength<=array.length; j++) {
int num = 0;
for (int k =0; k < array.length; k++) {
sum = sum+array[k];
num++;
//拼凑出一个子区间则计算其平均值
if (num == numLength){
if (sum/numLength == result){
record++;
}
num=0;
sum = 0;
if (numLength>=2){
k--;
}
}
}
sum=0;
j=0;
numLength++;
}
System.out.println(record);
}


查看4道真题和解析