程序设计在线评测(Online Judge)


问题 1034. -- SUM(n,m)

1034: SUM(n,m)

时间限制: 1 Sec  内存限制: 128 MB
提交: 217  解决: 54
[提交][状态][讨论版]

题目描述

Give two integers n and m, you should compute the sum of all the integers form n to m. In other words, you should compute:

SUM(n,m) = n + (n + 1) + (n + 2) + … + (m - 1) + m

输入

The first line contains the numbers of  cases.  Each case consists of a line containing the numbers n and m,  (-231<n<=m<231)

输出

For each case, output SUM(n,m) in one line. followed by a blank line. You may assume the result will be in the range of 64-bit signed integer.

样例输入

3
1 100
-11 10
-89173 938749341

样例输出

5050

-11

440625159107385260


提示

可参考教材例4-6。另外解答本题会用到如下提示中的内容。

Q: How to use 64-bit integer types ?

A: Server supports signed and unsigned 64-bit integers.

  • Signed 64-bit integer. Value range: -9223372036854775808 .. 9223372036854775807.































    Compiler GCC/G++ Pascal Visual C++ 6.0 Visual Studio 2005/2008/2010/2012/2013
    Type name long long int64 __int64

    long long


    or


    __int64


    Input scanf("%lld", &x);

    or

    cin >> x;
    read(x); scanf("%I64d", &x);

    scanf("%lld", &x);


    or


    scanf("%I64d", &x);


    Output printf("%lld", x); cout << x; write(x); printf("%I64d", x);

    printf("%lld", x);


    or


    printf("%I64d", x);




  • Unsigned 64-bit integer. Value range: 0 .. 18446744073709551615.































    Compiler GCC/G++ Pascal Visual C++ 6.0 Visual Studio 2005/2008/2010/2012/2013
    Type name unsigned long long qword unsigned __int64

    unsigned long long


    or


    unsigned __int64


    Input scanf("%llu", &x);

    or

    cin >> x;
    read(x); scanf("%I64u", &x);

    scanf("%llu", &x);


    or


    scanf("%I64u", &x);


    Output printf("%llu", x);

    or cout << x;
    write(x); printf("%I64u", x);

    printf("%llu", x);


    or


    printf("%I64u", x);






Q How can I read input data until the end of file ?

A You can do it for example like this:



























Language C C++ Pascal
To read numbers int n;

while(scanf("%d", &n) != EOF)

{

...

}
int n;

while (cin >> n)

{

...

}
var n: integer;

...

while not seekeof do

begin

read(n);

...

end;
To read characters int c;

while ((c = getchar()) != EOF)

{

...

}
char c;

while (cin.get(c))

{

...

}
var c: char;

...

while not eof do

begin

read(c);

...

end;
To read lines char line[1024];

while(gets(line))

{

...

}
string line;

while (getline(cin, line))

{

...

}
var line: string;

...

while not eof do

begin

readln(line);

...

end;

来源

[提交][状态][讨论版]