我正在运行这个程序
#include <stdio.h>
int main ()
{
int n;
int a = 1;
int b = 2;
int product;
int i;
printf("How many numbers of the sequence would you like \n");
scanf("%d",&n);
for (i=0;i<n;i++)
{
printf("%d\n",a);
product = a * b;
a = b;
b = product;
}
return 0;
}
当我输入 n = 3 时,结果是 1 2 2
为什么 ?我打算让它显示 1 2 4,我做错了什么?为什么打印 1 2 2 。
最佳答案
And why is it print 1 2 2 .
printf("%d\n",a);
的逐步跟踪:
i a b product
0 1 2 ?
1 2 2 2
2 2 4 4
3 4 8 8
4 8 32 32
关于C "for"循环问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32549879/