这是斐波那契数列,通项看下面。有了通项就很好编了啊。做一个循环累加就行了
#include
#include
int main(int argc, char *argv[])
{
int i=0,j=0,k=1,s=0,n=0,t=0;
printf("Fabonaci:output prior N numbers and their sum\nSo input N : ");
scanf("%d",&n);
for(i=0;i
printf(" %d",k);
s=s+k;
t=j; j=k; k=k+t;
}
printf("\nTotal=%d\n",s);
system("PAUSE");
return 0;
}
输出前n项及其和,看似不那么难哦!楼主,thinking is very important for us whom compile program 。
#include
int main()
{
int f1=1,f2=1;
int temp;
int sum=2;
int i;
for(i=3;i<=20;i++)
{
temp=f2;
f2=f2+f1;
sum+=f2;
f1=temp;
}
printf("sum=%d\n",sum);
return 0;
}
int i=1;
int add=0;
int swp;
int x=20;
int size=0;
while(x--){
i+=add;
size+=i;
add++;
}
return size;