|
该程序用递归的方法实现两数相乘:
#include <stdio.h>
int Multiply(int M, int N)
{
int result;
if( N == 1)
{
result = M;
return result;
}
else
{
result = M + Multiply(M,N-1);
}
return result;
}
int main(void)
{
int NumbA;
int NumbB;
int Product;
printf("please input NumbA for NumbA*NumbB:");
scanf("%d",&NumbA);
printf("please input NumbB for NumbA*NumbB:");
scanf("%d",&NumbB);
Product = Multiply(NumbA, NumbB);
printf("The Result is %d\n", Product);
return 0;
}
说明
处理递归问题,常采用 if 语句来判断是否符合递归结束条件,其算法格式如下:
if (符合递归结束条件) then
返回答案
else
使用递归将程序分割为更简单的小程序 |
|