1981-組合數

【C系列4.13】函數訓練之組合數 1981

Time Limit:  1 s      Memory Limit:   32 MB
Submission:187     AC:88     Score:19.06

 

Description

今天cyn學會了組合數的定義,然後興致勃勃的做好了mwy老師給的題目,你能幫她驗算一下嗎?(主函數已經給出,你只需要提交com函數)

1
2
3
4
5
6
7
8
9
10
11
12
13
#include<stdio.h>
int com(int x,int y);
int main()
{
    int a,b,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&a,&b);
        printf("%d\n",com(a,b));
    }
    return 0;
} 

Input

第一行輸入一個整數T,代表有T組測試樣例
接下來T行每行輸入兩個整數n,m(n,m <= 30)

Output

對於每一組輸入數據,輸出相應的答案

Samples

input:
45 36 48 529 15
output:
10
15
56
77558760

Hint

 可以用遞歸,或者需要簡化公式。


小提示:組合數



下附AC代碼:

#include<stdio.h>
int com(int x,int y);
int main()
{
    int a,b,t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&a,&b);
        printf("%d\n",com(a,b));
    }
    return 0;
}

int com(int x, int y) {
	return (x == y || 0 == y) ? 1 : com(x - 1, y) + com(x - 1, y - 1);
}


原題鏈接:http://acm.hznu.edu.cn/OJ/problem.php?cid=1092&pid=14

發佈了53 篇原創文章 · 獲贊 8 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章