user-0 提问: while语句计算加减法

回复:
user: user-1
content: 引用 10 楼 ipqtjmqj 的回复:[quote=引用 3 楼 zhao4zhong1 的回复:] 仅供参考：for (A;B;C) D; //等价于 {  A;  while (1) {   if (!(B)) break;   D;   C;[i]  } }  明明不等价http://codepad.org/NfncxIsE[/quote]  6.5.3 The for statement [stmt.for] 1 The for statement for ( for-init-statement condition; expression) statement is equivalent to { for-init-statement while ( condition ) { statement expression ; } } except that names declared in the for-init-statement are in the same declarative region as those declared in the condition, and except that a continue in statement (not enclosed in another iteration statement) will execute expression before re-evaluating condition. [ Note: Thus the first statement specifies initialization for the loop; the condition (6.4) specifies a test, made before each iteration, such that the loop is exited when the condition becomes false; the expression often specifies incrementing that is done after each iteration. — end note ]

user: user-2
content: 引用 13 楼 ipqtjmqj 的回复:[quote=引用 11 楼 zhao4zhong1 的回复:] [quote=引用 10 楼 ipqtjmqj 的回复:] [quote=引用 3 楼 zhao4zhong1 的回复:] 仅供参考：for (A;B;C) D; //等价于 {  A;  while (1) {   if (!(B)) break;   D;   C;  } }  明明不等价http://codepad.org/NfncxIsE[/quote] #define 里面变量不加括号，还带++ whilefun少一层大括号 [/quote] 只有带参数的宏，它的参数才可称为变量吧，否则只是字符串替换, 还有whilefun少一层大括号不影响结果吧。 我加了大括号，加了括号，结果还是和原来一样http://codepad.org/DyGOO9MG[/quote] 给你出道题： A是B， A不是B。 将上面的A和B分别用一个词来替换，使前后不矛盾。

user: user-3
content: 引用 11 楼 zhao4zhong1 的回复:[quote=引用 10 楼 ipqtjmqj 的回复:] [quote=引用 3 楼 zhao4zhong1 的回复:] 仅供参考：for (A;B;C) D; //等价于 {  A;  while (1) {   if (!(B)) break;   D;   C;  } }  明明不等价http://codepad.org/NfncxIsE[/quote] #define 里面变量不加括号，还带++ whilefun少一层大括号 [/quote] 只有带参数的宏，它的参数才可称为变量吧，否则只是字符串替换, 还有whilefun少一层大括号不影响结果吧。 我加了大括号，加了括号，结果还是和原来一样http://codepad.org/DyGOO9MG

user: user-4
content: #include <stdio.h> int main() {     int sum = 0,n;     printf("输入数字\n");     scanf("%d",&n);     if(n > 0)     {         while          {            if(n%2)               sum +=n;            else               sum -=n;            n--;           }       }       else        {           while(n)          {              if(n%2);                 sum +=n;              else                 sum -=n;              n++;            }         }         printf("和为%d\n",sum);         return 0;  	}

user: user-2
content: 引用 10 楼 ipqtjmqj 的回复:[quote=引用 3 楼 zhao4zhong1 的回复:] 仅供参考：for (A;B;C) D; //等价于 {  A;  while (1) {   if (!(B)) break;   D;   C;  } }  明明不等价http://codepad.org/NfncxIsE[/quote] #define 里面变量不加括号，还带++ whilefun少一层大括号

user: user-3
content: 引用 3 楼 zhao4zhong1 的回复:仅供参考：for (A;B;C) D; //等价于 {  A;  while (1) {   if (!(B)) break;   D;   C;  } }  明明不等价http://codepad.org/NfncxIsE

user: user-5
content: int sum_ex(int n) { 	static int sum = 0; 	sum = sum+n*(n%2 ? 1 : -1); 	return (n == 0)? sum:sum_ex(n-1); }

user: user-6
content: int getSum(int n) { 	if (n <= 0) 	{ 		return 0; 	} 	int i, sum; 	i = 1, sum = 0; 	while (i<=n) 	{ 		if (i % 2) 		{ 			sum += i; 		} 		else 		{ 			sum -= i; 		} 		i++; 	} 	return sum; }  当然，可以直接总结一公式~

user: user-0
content: 感谢 大家的回复,谢谢

user: user-0
content: 引用 1 楼 billowyu 的回复:int input; 	printf("Please input an integer: "); 	scanf("%d",&input); 	int i = 1; 	int count = 0; 	while (1) 	{ 		if (i > input) 			break;  		if (i%2 == 0) { 			count -= i; 		} else { 			count += i; 		} 		i++; 	}  	printf("%d", count);    感谢!!

user: user-7
content: 忘记写返回了：  int Sum(int n) { int ret=0; for(int i=0;i<n;i++) {     ret+=((i<0)?1:-1)*i; } return ret; }

user: user-7
content: int Sum(int n) { int ret=0; for(int i=0;i<n;i++) {     ret+=((i<0)?1:-1)*i; } }

user: user-2
content: 仅供参考：for (A;B;C) D; //等价于 {  A;  while (1) {   if (!(B)) break;   D;   C;  } }

user: user-8
content: int input; 	printf("Please input an integer: "); 	scanf("%d",&input); 	int i = 1; 	int count = 0; 	while (1) 	{ 		if (i > input) 			break;  		if (i%2 == 0) { 			count -= i; 		} else { 			count += i; 		} 		i++; 	}  	printf("%d", count);

user: user-9
content: #include <stdio.h> #include <math.h> int main() { 	int n; 	int sum=0; 	printf("请输入数字"); 	scanf("%d",&n); 	for(int i=1;i<=n;i++) 	sum+=i*pow(-1,i+1); 	printf("%d",sum); }