正确答案为 b, 要是你的参考答案是d ,可能有误
答案是B。
#include
int main()
{
int a = 1, b = 2;
int cnt = 0;
while (a < 6)
{
printf("------------Loop %02d-------------\n", ++cnt);
printf("a = %d, b = %d\n", a, b);
printf("b += a; \n");
b += a;
printf("a = %d, b = %d\n", a, b);
printf("a += 2; \n");
a += 2;
printf("a = %d, b = %d\n", a, b);
printf("b %%= 10; \n");
b %= 10;
printf("a = %d, b = %d\n", a, b);
}
printf("\n==============================\n");
printf("Final result: a = %d, b = %d\n", a, b);
return 0;
}
运行结果:
------------Loop 01-------------
a = 1, b = 2
b += a;
a = 1, b = 3
a += 2;
a = 3, b = 3
b %= 10;
a = 3, b = 3
------------Loop 02-------------
a = 3, b = 3
b += a;
a = 3, b = 6
a += 2;
a = 5, b = 6
b %= 10;
a = 5, b = 6
------------Loop 03-------------
a = 5, b = 6
b += a;
a = 5, b = 11
a += 2;
a = 7, b = 11
b %= 10;
a = 7, b = 1
==============================
Final result: a = 7, b = 1
明显是答案印刷有误,不必迷信答案,遇到这种问题自己用编译器运行一下就一目了然了。此题很简单,答案为B无疑。
结果 B
循环1:a=1. b=3;a=3,b=3
循环2:a=3. b=6;a=5,b=6
循环3:a=5. b=6;a=7,b=1
退出循环 a=7。
此时 a=7 b=1
要相信自己啊,我的同志,“正确”答案又能怎样呢?