题解 | #整数和#

整数和

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;
}

全部评论

相关推荐

喜欢走神的孤勇者练习时长两年半:爱华,信华,等华,黑华
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务