H - the Sum of Cube(水题)

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

InputThe 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 range[A,B].OutputFor 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

 

首先要明白这一题的题意,就是求一个范围内的所有整数数的立方和

注意:数据范围要用 long long

   求立方和可以用pow函数——>pow(j, 3)

感觉第二个数据错了…………

 

AC代码

#include<stdio.h>

int solve(int x)
{
    return x*x*x;
}

int main()
{
    int t;
    int num = 0;
    scanf("%d", &t);
    while(t--)
    {
        int a, b;
        long long sum = 0;

        scanf("%d %d", &a, &b);
        for(int i = a; i <= b; i++)
        {
            sum += solve(i);
        }
        num++;
        printf("Case #%d: %lld\n", num, sum);
    }
    return 0;
}
View Code

 

posted @ 2018-03-08 15:19  Veritas_des_Liberty  阅读(145)  评论(0编辑  收藏  举报