京东8.21测开代码
第一题:对角线邻居数:
public class test1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int []x = new int[n]; int []y = new int[n]; for(int i =0;i<n;i++){ x[i] = sc.nextInt(); y[i] = sc.nextInt(); } int res = 0; for(int i =0;i<n-1;i++){ for(int j=i+1;j<n;j++){ if(Math.abs(x[i]-x[j])== Math.abs(y[i]-y[j])){ res++; } } } System.out.println(res); } }只过了70多,怀疑是不是漏了只有一栋房子没有邻居的情况...
第二题中庸数全A:
public class test2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int []num = new int[n]; int []judge = new int[n]; for(int i =0;i<n;i++){ num[i] = sc.nextInt(); judge[i] = sc.nextInt(); } ArrayList<Integer> yes = new ArrayList<>(); ArrayList<Integer> no = new ArrayList<>(); for(int i = 0;i<n;i++){ if(judge[i]==0) { //表示介意 no.add(num[i]); } else {//表示不介意成为最大值的 yes.add(num[i]); } } int max =yes.get(0); int min =yes.get(0); for(int i =0;i<yes.size();i++){ if(max<yes.get(i)){ max = yes.get(i); } if(min>yes.get(i)){ min = yes.get(i); } } int res =0; for(int i =0;i<no.size();i++){ if(min<no.get(i)&&max>no.get(i)){ res++; } } for(int i =0;i<yes.size();i++){ if(min<yes.get(i)&&max>yes.get(i)){ res++; } } System.out.println(res+2); } }