第一题
我的代码有问题,但我不知道哪错了希望能看下
import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { List<int[]> num=new ArrayList<>(); Scanner sc=new Scanner(System.in); int t=sc.nextInt(); for (int i = 0; i < t; i++) { int n=sc.nextInt(); int[] ints=new int[n]; for (int j = 0; j < n; j++) { ints[j]=sc.nextInt(); } num.add(ints); } sc.close(); int[] res=new int[t]; int index=0; for (int[] ints : num) { Arrays.sort(ints); for (int i = ints.length-1; i >0 ; i--) { res[index]+=ints[i]; } res[index++]+=ints[0]*(ints.length-2); } for (int re : res) { System.out.println(re); } } }第二题
最后一下才解决,没来得及提交,之前的只过了10%,如果不对,请指出一下
import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc= new Scanner(System.in); int n=sc.nextInt(); int[] degree=new int[n]; for (int i = 0; i < n; i++) { degree[i]=sc.nextInt(); } sc.close(); //奇数小丽赢,偶数小强赢 Arrays.sort(degree); int m=(n*(n-1))/2; int res=0; if ((m&1)==1){ for (int i = 0; i <n/2 ; i++) { for (int j = degree.length-1; j >i; j--) { res+=degree[i]*degree[j]; } } }else { for (int i = degree.length-1; i >n/2 ; i--) { for (int j = 0; j < i; j++) { res+=degree[i]*degree[j]; } } } System.out.println(res); } }
全部评论
(5) 回帖