发布网友 发布时间:2024-03-09 00:57
共3个回答
热心网友 时间:2024-03-11 03:25
#include <stdio.h>
int main() {
int a = 1, b = 2, sum = 0;
for (int i = 1; i <= 20 / 2; i++) {
sum += a * b - b * (a + b);
a = a + b;
b = a + b;
}
printf("%d\n", sum);
return 0;
}
热心网友 时间:2024-03-11 03:28
#include<stdio.h>
#include<math.h>
int main()
{
long a=1,b=1,t=1,sum=0;
for(int i=1;i<=20;i++)
{
if(i%2==0)
a=-a;
b=b+abs(t);
sum+=a*b;
printf("a= %-10d b= %-10d sum= %-10d\n",a,b,sum);//a,b,sum的变化情况
t=a;
a=b;
}
printf("前20项的和是:%ld",sum);
return 0;
}
热心网友 时间:2024-03-11 03:32
据观察,与 斐波那契数列 有关
int i=1,t=0,a,b,s=1;
long r;
while(i<=20)
{ if (i<3)
f[i]=1;
else
f[i]=f[i-2]+f[i-1];
if (i==1)
a=1;
else
a=b;
b = a + f[i];
t = s*a*b;
r += t;
printf("%3d %8d %8d %8d %10d\n",i,s,a,b,r);
s = -s;
i++;
}