题解 | #牛牛学数列#
牛牛学数列
https://www.nowcoder.com/practice/0b97367cd2184c12a0e02f7c223aee11
#include <stdio.h> int main() { int n; int sum = 0; int tmp; while (scanf("%d", &n) != EOF) { // 注意 while 处理多个 case for(int i=1; i<=n; i++){ tmp = i; //用一个变量暂存i的值 if(i % 2 == 0){//偶数则变成-i tmp = -i; } sum = sum + tmp;//求和 } printf("%d\n", sum); } return 0; }