百度笔试
1.输入n个数,不是0就是5,输出能被90整除的最大的数,不存在则输出-1;
public class Main1 {
public static void main(String[] args) {
// Scanner sc = new Scanner(System.in);
// int n = sc.nextInt();
// int[] arr = new int[n];
// int c0 = 0;
// int c5 = 0;
// for (int i = 0; i < n; i++) {
// arr[i] = sc.nextInt();
// if (arr[i] == 0) c0++;
// else c5++;
// }
int c0=1;
int c5=10;
c5 = c5 / 9;
if (c0 == 0) System.out.println(-1);
if (c5 < 1) System.out.println(0);
StringBuilder sb = new StringBuilder("");
while (c5-- > 0) {
sb.append("555555555");
}
while (c0-- > 0) {
sb.append("0");
}
System.out.println(sb.toString());
}
}
2.满足全部指标的奶牛成为优质奶牛。
输入:n(奶牛头数编号为1-n),m(指标个数),k(满足指标的区间数)
10,2
2
[1,2]
[6,8]
1
[1,4]
输出:优质奶牛的头数,从小到大的编号
2
1,2
import java.util.*;
public class Main2 {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNextInt()){
int t=sc.nextInt();
for(int i=0;i<t;i++) {
int n = sc.nextInt();
int m = sc.nextInt();
int[] arr=new int[n];
for(int j=0;j<m;j++){
int k=sc.nextInt();
for(int s=0;s<k;s++){
int a=sc.nextInt();
int b=sc.nextInt();
for(int x=a;x<=b;x++){
arr[x-1]++;
}
}
}
int count=0;
List<Integer> res=new ArrayList<>();
for(int j=0;j<n;j++){
if(arr[j]>=m){
count++;
res.add(j+1);
}
}
System.out.println(count);
for(int j=0;j<res.size();j++){
System.out.print(res.get(j)+" ");
}
}
}
}
}
浙公网安备 33010602011771号