#include <stdio.h> int main() { int sum = 0; int i = 1; while (i<=100) { i++; if (i%2 != 0) { sum = sum + i; } else { sum = sum - i; } } printf("sum = %d\n",sum); return 0; }
你是不是想求1-2+3-4.....100,可是你这个,-2+3-4...101,建议定义flag作为符号变量