本帖最后由 jackz007 于 2021-10-23 00:12 编辑
只要把代码稍作修改便可看出原因#include <stdio.h>
int main(void)
{
int a = 1 , b = 2 , c = 0 , sum = 1 ;
while((c < a || c < b) && b < 4000000) {
c = a + b ;
if(c % 2 == 0) sum += c ;
if((a < b || a < c) && c < 4000000) {
a = b + c ;
if(a % 2 == 0) sum += a ;
if((b < a || b < c) && a < 4000000) {
b = a + c ;
if(b % 2 == 0) sum += b ;
}
}
printf("a = %8d b = %8d c = %8d\n" , a , b , c) ;
}
printf("sum = %d\n" , sum) ;
}
编译、运行实况:D:\00.Excise\C>g++ -o x x.c
D:\00.Excise\C>x
a = 5 b = 8 c = 3
a = 21 b = 34 c = 13
a = 89 b = 144 c = 55
a = 377 b = 610 c = 233
a = 1597 b = 2584 c = 987
a = 6765 b = 10946 c = 4181
a = 28657 b = 46368 c = 17711
a = 121393 b = 196418 c = 75025
a = 514229 b = 832040 c = 317811
a = 2178309 b = 3524578 c = 1346269
a = 2178309 b = 3524578 c = 5702887
sum = 4613731
D:\00.Excise\C>
请关注程序输出的倒数第2行:a = 2178309 b = 3524578 c = 5702887
再看看循环条件 while((c < a || c < b) && b < 4000000) {
是的,就是因为 (c < a || c < b) 的条件已经不成立了,所以,尽管 b = 5702887 远远不到 4000000,可是循环却还是终止了。 |