美文网首页
简单的水题

简单的水题

作者: portability | 来源:发表于2018-08-09 16:21 被阅读0次

    网易牛分苹果

    import java.util.Arrays;
    import java.util.Scanner;
    
    public class Main {
        public static void main(String[] args){
            Scanner scanner = new Scanner(System.in);
            int n = scanner.nextInt();
            int[] cow_apples = new int[n];
            int i = 0, sum = 0, tmp = 0;
            while (i < n){
                tmp = scanner.nextInt();
                cow_apples[i++] = tmp;
                sum += tmp;
            }
            if (sum % n != 0){
                System.out.println(-1);
                return;
            }
            int target = sum / n;
            Arrays.sort(cow_apples);
            int count = 0;
            for (int j:
                    cow_apples){
                if (((j - target) & 1) == 1){
                    System.out.println(-1);
                    return;
                }
                if (j < target){
                    count += ((target -  j) >> 1);
                }
            }
            System.out.println(count);
        }
    }
    

    相关文章

      网友评论

          本文标题:简单的水题

          本文链接:https://www.haomeiwen.com/subject/elrcbftx.html