1. 程式人生 > >C語言訓練-1149-計算題

C語言訓練-1149-計算題

Problem Description
一個簡單的計算,你需要計算f(m,n),其定義如下:
當m=1時,f(m,n)=n;
當n=1時,f(m,n)=m;
當m>1,n>1時,f(m,n)= f(m-1,n)+ f(m,n-1)
Input
第一行包含一個整數T(1<=T<=100),表示下面的資料組數。
以下T行,其中每組資料有兩個整數m,n(1<=m,n<=2000),中間用空格隔開。
Output
對每組輸入資料,你需要計算出f(m,n),並輸出。每個結果佔一行。
Sample Input
2
1 1
2 3
Sample Output
1
7

#include<bits/stdc++.h>  //C++萬能標頭檔案
int f(int m,int n)      //定義一個函式頭
{
	if(m==1)            當M==1時返回N的值
	{
		return n;
	}
	if(n==1)         //當N==1時返回M的值
	{
		return m;
	}
	if(m>1&&n>1)      //當M大於1並且N大於1的時候
	{
		return f(m-1,n)+f(m,n-1);   重複呼叫自身直到達到M==1 和 N==1的要求
	}
}
int main()
{
	
	int x,y,a;
	scanf("%d",&a);
	while(a--){
	scanf("%d %d",&x,&y);
	printf("%d\n",f(x,y));
	}return 0;
}