求立方和

Link: https://vjudge.net/contest/172540#problem/H

Description:

A range is given, the begin and the end are both integers. You should sum the cube of all the integers in the range.

Input

The first line of the input is T(1 <= T <= 1000), which stands for the number of test cases you need to solve.
Each case of input is a pair of integer A,B(0 < A <= B <= 10000),representing the rangeA,BA,B.

Output

For each test case, print a line “Case #t: ”(without quotes, t means the index of the test case) at the beginning. Then output the answer – sum the cube of all the integers in the range.

Sample Input

2
1 3
2 5

Sample Output

Case #1: 36
Case #2: 224

思路:

求1~n的立方和公式(即求体积公式):

    sum=(n*(n+1)/2)^2;
    [体积=边长^2]

如求1~4的立方和:

    sum=(4*5/2)*(4*5/2)=100

Code:

#include
#include
#include
#include
int main()
{
    int t,a,b,c=0,s1,s2;
    scanf("%d",&t);
    while(t--)
    {
        c++;
        scanf("%d%d",&a,&b);
        a-=1;
        s1=(a*(a+1)/2)*(a*(a+1)/2);
        s2=(b*(b+1)/2)*(b*(b+1)/2);
        printf("Case #%d: %d\n",c,s2-s1);
    }
return 0;
}

你可能感兴趣的:(HPU2017)