4-10的任务代码怎么输
这题就是考对这三种循环的熟练度,你可以任选一种来写,当然也可以三种一起用,不过每个循环结束后要记得将sum和i重新初始化
include <stdio.h>
int main()
{
int sum = 0;
int i = 1;
int flag = 1;
while(i<=100)
{
i++;
sum+=i*flag;
flag*=-1;
}
printf("sum=%d\n",sum);
return 0;
}
int sum=0;
int i = 1;
int flag = 1;
do{
i++;
sum+=i*flag;
flag*=-1;
}while(i<=100);
printf("sum=%d\n",sum);
return 0;
}
i = 1;
int sum = 0;
int i = 1;
int flag = 1;
for(i=1;i<=100;i++)
{sum+=i*flag;
flag*=-1;
}
printf("sum=%d\n",sum);
return 0;
}