hdu-1205 喫糖果

喫糖果

這裏寫圖片描述


這個問題用到了抽屜原理,先找出給出的N個數中最大的那個,依次排開,則共有N+1個空,把其他的數插入到這N+1個空位中。若其他糖果數之和大於等於最大糖果數-1則滿足條件。
這裏寫圖片描述


注意:用Java寫該題代碼一定要用大數,樓主因爲沒用大數被坑了好幾次!!

import java.math.BigInteger;
import java.util.Scanner;

public class p1205{//注意該題要用大數
                   //該題用到了鴿籠原理
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        while (t-- > 0) {
            int n = sc.nextInt();
            BigInteger max =new BigInteger("0");
            BigInteger sum =new BigInteger("0");
            for (int i = 0; i < n; i++) {
                BigInteger x= sc.nextBigInteger();
                if (x.compareTo(max)!=-1) {
                    max =x;
                }
                sum=sum.add(x);
            }
            sum = sum.subtract(max);
            if (sum.compareTo(max.subtract(new BigInteger("1")))!=-1) {
                System.out.println("Yes");
            } else {
                System.out.println("No");
            }
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章