C语言 速度球教

来源:百度知道 编辑:UC知道 时间:2024/07/01 07:00:36
create a c program calculate the triangular number of the users request. read from the keybard using scanf(). a triangular number is the sum of the preceding numbers, so the triangular number 7 has a value of 7+6+5+4+3+2+1
要求用递归做。。。。

#include <stdio.h>
int sjxs(int a)
{
int n;
if (a==0) return sjxs(n);
else
{

n=n+a+sjxs(a-1);

}
int main ()
{
int b;
printf("please input a number");
scanf("%d",&b);
printf(" the answer is %d\n",sjxs(b);
return 0;
}
主要是累加量和减少量的设定,还有递归完成后的出口

main(){
int s=0,n;
scanf("%d",&n);
while (n)s+=n--;n
printf("%d\n",s);
}