[Java] d950. A. 帕斯卡三角形

常見的題目
import java.io.BufferedInputStream;
import java.util.Scanner;
 
public class D950 {
    public static void main(String[] args) {
        Scanner in = new Scanner(new BufferedInputStream(System.in));
        while(in.hasNext()) {
            int count = in.nextInt();
            for(int a = 0; a < count; a++) {
                int n = in.nextInt() - 1;
                int k = in.nextInt() - 1;
                if(k == 0 || n == k) {
                    System.out.println(1);
                } else {
                    k = ((n-k) < k)? k : (n-k);
                    int comb = 1;
                    for(int b = n; b > k; b--) {
                        comb *= b;
                    }
                    for(int c = (n-k); c >= 1; c--) {
                        comb /= c;
                    }
                    System.out.println(comb);
                }
            }
        }
        in.close();
    }
}

沒有留言:

張貼留言