滴滴笔试9月4日
第一题: 桃子称重
小昱家的桃园丰收了!小昱采摘下来n个桃子,并对这些桃子称重,其中第i个桃子的重量为ai。
小昱想选择一些桃子装成一箱后送给朋友,但是小昱不希望一箱桃子中有一些太大的桃子而影响整体美观。于是他给装箱工人提出了一个要求:一箱桃子中最重的桃子重量不能超过平均重量的k倍。
装箱工人想知道在满足小昱要求的情况下,一箱最多能装多少个桃子。
public class PeachBox {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
int n=scanner.nextInt();
int k=scanner.nextInt();
Long[] arr=new Long[n];
for (int i = 0; i < arr.length; i++) {
arr[i]=scanner.nextLong();
}
Arrays.sort(arr);
int count=0;
int sum=0;
int left=0;
// System.out.println(Arrays.toString(arr));
for (int i = 0; i < arr.length; i++) {
sum+=arr[i];
while (arr[i]>sum*1.0/(i-left+1)*k){
count=Math.max(i-left,count);
sum-=arr[left];
left++;
}
}
System.out.println(count);
}
} 第二题: 美数课
老张教授开了一堂美数课!老张认为每个非负整数x都有一个美丽值b(x)。
一个非负整数的美丽值定义为这个数十进制下每个数位的异或和。
即,对于123来说,美丽值为1^2^3=0,对于654815424美丽值为6^5^4^8^1^5^4^2^4=9 (在C/C++中^运算符表示异或)
现在老张想考考同学,对于[L,R]这个闭区间内的所有整数,美丽值恰好为t的数有多少个。
public class Main {
int b(int num){
int res=0;
while (num!=0){
int tmp=num%10;
num=num/10;
res=res^tmp;
}
return res;
}
public static void main(String[] args) {
Main main =new Main();
Scanner scanner=new Scanner(System.in);
int n = scanner.nextInt();
int[] left=new int[n];
int[] right=new int[n];
int[] t=new int[n];
for (int i = 0; i < n; i++) {
left[i]=scanner.nextInt();
}
for (int i = 0; i < n; i++) {
right[i]=scanner.nextInt();
}
for (int i = 0; i < n; i++) {
t[i]=scanner.nextInt();
}
for (int i = 0; i < n; i++) {
int count=0;
for (int j = left[i]; j <= right[i]; j++) {
if (main.b(j)==t[i])
count++;
}
System.out.print(count+" ");
}
}
} 直接暴力75%