题目描述
编写程序,读入一个整数N。若N为非负数,则计算N 到2N 之间的整数和;若N为一个负数,则求2N 到N 之间的整数和。
输入描述:
第一行表示样例数m,接下来m行每行一个整数N,N的绝对值不超过100。
输出描述:
输出m行,每行表示对应的题目所求。
输入
2
2
-1
输出
9
-3
备注:
注意N可能为负数
#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string>
using namespace std;
int main()
{
int n,i,j,x,s;
cin>>n;
for(i=0;i<n;i++)
{
cin>>x;
s=0;
if(x>=0)
{
for(j=x;j<=2*x;j++)
s+=j;
}
else
{
for(j=x*2;j<=x;j++)
s+=j;
}
cout<<s<<endl;
}
return 0;
}