艺龙题解JAVA

艺龙

第一题:幸运数列

4
1 4 2 3

"Luck sequence"
5
6 -1 5 3 7

"Unluck sequence"
描述:第一个数为序列个数,后面n-1个数为序列数

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int len = sc.nextInt();
        int[] arr = new int[len];
        for(int i=0;i<len ;i++){
            arr[i] = sc.nextInt();
        }
        int[] a = new int[len];
        boolean flag = true;
        int count = 0;
        here:
        for(int i=1;i<len;i++){
            int now = Math.abs(arr[i]-arr[i-1]);
            if(now>=len){
                flag = false;
                break here;
            }else{
                if(a[now]==0){
                    count++;
                    a[now]=1;
                }
            }
        }
        if(a[0]!=0)
            flag = false;
        System.out.println(flag && count == len-1?"Luck sequence":"Unluck sequence");
    }

第二题

1
4
1 10 100 1000
100 10 1000 1
10 10 10 10
50 5 15 55
结果: 1+5+10+1 = 17
第一行为测试用例组数,第二行为n,下面四行每行n个数。求最小权值

public static void main(String[] args) {
        Scanner sc =new Scanner(System.in);
        int num=  sc.nextInt();
        int[] a ; //数据
        char[] f ; //方向
        while(num-->0){
            int n = sc.nextInt();
            a = new int[n];
            f = new char[n];
            Arrays.fill(a,Integer.MAX_VALUE);
            for(int i=0;i<4;i++){
                for(int j=0;j<n;j++){
                    int now = sc.nextInt();
                    int cmp = a[j];
                    a[j] = Math.min(now, a[j]);
                    if(a[j]!=cmp){
                        if(i==0)
                            f[j] = 'E';
                        else if(i==1)
                            f[j] = 'S';
                        else if(i==2)
                            f[j] = 'W';
                        else 
                            f[j] = 'N';
                    }
                }
            }
            long sum = 0;
            for(int i=0;i<a.length;i++){
                sum += a[i];
            }
            System.out.println(String.valueOf(sum));
            System.out.println(new String(f));
        }
    }

2AC,有更好思路的牛友请指出啊。大佬勿喷 (逃

全部评论
可以的老铁
点赞 回复 分享
发布于 2017-09-26 22:29
谢谢分享
点赞 回复 分享
发布于 2017-09-26 22:10
很66666啊。。。。。
点赞 回复 分享
发布于 2017-09-26 21:52

相关推荐

05-09 12:23
已编辑
华南理工大学 Java
野猪不是猪🐗:给他装的,双九+有实习的能看的上这种厂我直接吃⑨✌们拿它练练面试愣是给他整出幻觉了
点赞 评论 收藏
分享
Cherrycola01:0实习 0项目 约等于啥也没有啊 哥们儿这简历认真的吗
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务