现在的位置: 首页 > 综合 > 正文

POJ 2924

2016年09月26日 ⁄ 综合 ⁄ 共 571字 ⁄ 字号 评论关闭
//POJ 2924 等差公式求和 
#include <iostream>
#include <cmath>
using namespace std;

int main()
{
	long long a;
	long long b;
	int n;
	cin>>n;
	while(n && !cin.eof())
	{
		for(int i = 0; i < n; i++)
		{
			long long sum = 0;
			cin>>a>>b;
			cout<<"Scenario #"<<i+1<<":"<<endl;
			if( a > 0)
			{
				sum += (a+b) * (b-a+1) / 2;
				cout<<(long long)sum<<endl<<endl;
			}
			else
			{
				if(-a > b || -a > -b)
				{
					if(b > 0)
					{
						long long diff = -(a + 

b);
						sum += (a - (b+1)) * 

diff / 2;
						cout<<(long long)

sum<<endl<<endl;
					}
					else
					{
						sum += (a+b) * (b-a+1) 

/ 2;
						cout<<(long long)

sum<<endl<<endl;
					}
				}
				else if (-a < b)
				{
					if(b > 0)
					{
						long long diff = a + b;
						sum += (-a + 1 + b) * 

diff / 2;  
						cout<<(long long)

sum<<endl<<endl;
					}
				}
				else{
					cout<<0<<endl<<endl;
				}
			}
		}
		cin>>n;
	}
	return 0;
}

【上篇】
【下篇】

抱歉!评论已关闭.