您的位置:首页 > 编程语言

暑期集训之立方求和

2017-07-21 20:47 246 查看
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
这道题虽然看上去非常简单,就是一个先立方再求和的原理,但实际上这道题的难点就是数据类型的考虑上,int型数据明显不够,所以在这里运用了double型数据,AC,代码如下:
#include<stdio.h>
#include<math.h>
main()
{
int t,f;
scanf("%d",&t);
f=t;
while(t--)
{
int m,n;
double i,b,sum=0;
scanf("%d%d",&m,&n);
for(i=m;i<=n;i++)
{   b=i*i*i;
sum=sum+b;
}
printf("Case #%d: %.0lf\n",f-t,sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  编程 acm水题