题解 | #整数和#

整数和

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

全部评论

相关推荐

威猛的小饼干正在背八股:挂到根本不想整理
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务