题解 | 元素方碑

元素方碑

https://www.nowcoder.com/practice/5c6e7ed4726e41f4ac99a4dedf1e5bb2

import java.util.Scanner;
import java.util.ArrayList;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int times=0;
        if(in.hasNext()){
            times=in.nextInt();
        }
        for(int i=0; i<times && in.hasNextLine(); i++){
            
            int size = in.nextInt();
            if(in.hasNextLine()){
                ArrayList<Integer> list = new ArrayList<>();
                while(in.hasNextInt() && list.size()<size){
                    list.add(in.nextInt());
                }
                test(list);
            }
            
        }
        // 注意 hasNext 和 hasNextLine 的区别
    //    while (in.hasNextInt()) { // 注意 while 处理多个 case
    //         int a = in.nextInt();
    //         int b = in.nextInt();
    //         System.out.println(a + b);
    //     }
    }

    private static void test(ArrayList<Integer> list){
        if(list.size()<2){
             System.out.println("YES");
            return;
        }
      
        int sum1=0;
        int count1=0;
        int sum2=0;
        int count2=0;
        int sum=0;

        for(int i=0;i<list.size();i++){
            sum += list.get(i);
            if(i%2==0){
                sum1 += list.get(i);
                count1++;
            }else{
                sum2 += list.get(i);
                count2++;
            }
        }
        if(sum1%count1==0 && sum2%count2==0 && sum%list.size()==0 && sum1/count1==sum2/count2){
             System.out.println("YES");
        }else{
             System.out.println("NO");
        }
    }
}

全部评论

相关推荐

狸猫换offer:神通广大的互联网
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务