C 计算组合数 SDUT

Time Limit: 1000 ms Memory Limit: 32768 KiB


Problem Description

计算组合数。C(n,m),表示从n个数中选择m个的组合数。
计算公式如下:
若:m=0,C(n,m)=1
否则, 若 n=1,C(n,m)=1
否则,若m=n,C(n,m)=1
否则 C(n,m) = C(n-1,m-1) + C(n-1,m).


Input

第一行是正整数N,表示有N组要求的组合数。接下来N行,每行两个整数n,m (0 <= m <= n <= 20)。


Output

输出N行。每行输出一个整数表示C(n,m)。


Sample Input

3
2 1
3 2
4 0


Sample Output

2
3
1

扫描二维码关注公众号,回复: 8601013 查看本文章

Hint
Source


#include <stdio.h>
#include <stdlib.h>
  int f(int n,int m)
  {
      int a;
      if(m==0)
        a = 1;
      else if(n==1)
        a = 1;
      else if(m==n)
        a = 1;
      else a = f(n-1,m-1)+f(n-1,m);
      return a;

  }
  int main()
  {
      int i,n;
      int a,b;
      scanf("%d",&n);
      for(i=1;i<=n;i++)
      {
          scanf("%d%d",&a,&b);
         int sum = f(a,b);
         printf("%d\n",sum);
      }

      return 0;
  }

发布了136 篇原创文章 · 获赞 95 · 访问量 2343

猜你喜欢

转载自blog.csdn.net/zhangzhaolin12/article/details/103930230