uva 530 – binomial showdown

Contents

Problem

題目網址
中文網址

Solution

邊乘邊除,防止途中溢位。

Code

UVa 530UVa 530 - Binomial Showdown
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20


int ()
{
int n, m;
while (scanf("%d%d", &n, &m) && n)
{
if (n - m < m)
m = n - m;
double ans = 1;
for (int i = n, j = 1; j <= m; j++,i--)
{
ans *= i;
ans /= j;//從小的開始除,最終答案是整數,所以最後一定除的盡
}
printf("%.0lfn", ans);
}

return 0;
}