怎么算出的結(jié)果不一樣?

DF49
2017-12-22
2 回答
舉報(bào)
0/150
提交
取消
2017-12-22
舉報(bào)
2017-12-24
我改了一下你的代碼。
有什么再問(wèn)吧
2017-12-22
#include <stdio.h>
int main()
{
??? /* 小伙伴們:
?????????? 選擇你們認(rèn)為最合理的循環(huán)結(jié)構(gòu)完成功能吧 */
??? int sum = 0;? //定義計(jì)算結(jié)果變量sum
??? int i = 1;??? //定義循環(huán)數(shù)字變量i
??? int flag = 1; //定義符號(hào)狀態(tài)變量flag
???
??? //使用while循環(huán)
??? while(i<=100)
??? {
??????
??????? sum+=i*flag;
??????? i++;
??????? flag*=-1;
??? }
???
??? i = 1;? //重新初始化變量i
??? sum = 0;
??? //do-while循環(huán)
??? do{
???????? //printf("i=%d\n",i*flag);
??????? sum=sum+(i*flag);
??????? //printf("sum=%d\n",sum);
??????? i++;
??????? flag*=-1;
??? }while(i<=100);
???
??? i = 1;? //重新初始化變量i
??? sum = 0;
??? //使用for循環(huán)
??? for(i=1;i<=100;i++)
??? {
?????? sum+=i*flag;
??????? flag*=-1;
??? }?
????
??? printf("sum=%d\n",sum);
??
??? return 0;???
}