首页 > 阿里8.21笔试题
头像
学习中的白小飞
编辑于 2020-08-21 20:04
+ 关注

阿里8.21笔试题

第一题
我的代码有问题,但我不知道哪错了希望能看下
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) 回帖
加载中...
话题 回帖

推荐话题

相关热帖

历年真题 真题热练榜 24小时
技术(软件)/信息技术类
查看全部

近期精华帖

热门推荐