题解 | #整数和#
整数和
https://www.nowcoder.com/practice/bba8c89fddf7462e8c6aae4791b36e23
#include <iostream> using namespace std; int sum1 (int x){ //x>0 int sum = 0; for (int i = x; i <= 2 * x; i++){ sum += i; } return sum; } int sum2 (int x){ //x<0 int sum = 0; for (int i = 2 * x; i <= x; i++){ sum += i; } return sum; } int main () { int m, x; while (cin >> m){ for (int i = 0; i < m; i++){ cin >> x; if (x > 0){ cout << sum1(x) << endl; }else if (x < 0){ cout << sum2(x) << endl; }else{ cout << "0" << endl; } } } return 0; }