题解 | 元素方碑

元素方碑

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int t = in.nextInt();
        for(int i = 0;i<t;i++){
            int n=in.nextInt();
            int[] nums = new int[n];
            int count = 0;
            for(int j = 0;j<n;j++){
                nums[j]=in.nextInt();
                count+=nums[j];
            }
            // 1.判断总能量是否为n的倍数
            if(count%n!=0){
                System.out.println("NO");
            }else{
                for(int j=0;j<n;j++){
                    nums[j]-=(count/n);
                }
                for(int j=1;j<n-1;j++){
                    nums[j+1]+=nums[j-1];
                    nums[j-1]=0;
                }
                boolean mark = true;
                for(int j=0;j<n;j++){
                    if(nums[j]!=0){
                        mark=false;
                        break;
                    }
                }
                System.out.println(mark?"YES":"NO");
            }
        }
    }
}

全部评论

相关推荐

09-23 14:45
贵州大学 财务
勇敢求职牛牛:怎么9.2佬人手一个中信证券实习
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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