Poj2000解题报告:
|
x
|
1
|
2
|
3
|
4
|
5
|
6
|
7
|
8
|
9
|
10
|
|
n
|
1
|
2
|
2
|
3
|
3
|
3
|
4
|
4
|
4
|
4
|
|
11
|
12
|
13
|
14
|
15
|
16
|
17
|
18
|
19
|
……
|
|
5
|
5
|
5
|
5
|
5
|
6
|
6
|
6
|
6
|
……
|
这其实是一道数学题:
可以看出规律来,能得到的金币数是1*1+2*2+3*3+…= (n(n+1)(2n+1))/6。
所以我们要根据所给的输入求出n。可以很明显的看出来相同的n中最后一个所对应的数字x=n(n+1)/2 所以用求根公式解出n来,但是不一定求出整
数,所以要用到ceil(n)函数(求出比i大的最小整数,在<math.h>中)。n和ceil(n)作比较如果相等的话就是可以整除,运用上面的公式;要是不相
等,要减去多计算的天数。(c*(c+1)*(2*c+1))/6-((c*(c+1)/2)-a)*c(为了使公式简单设c=ceil(n))
以下是我的代码:
#include<stdio.h>
#include <math.h>
int main()
{ int a,b;
double n,c;
while(1)
{
scanf("%d",&a);
if(a==0) break;
n=(sqrt(1.0+8*a)-1.0)/2;
c=ceil(n);
if(n==c)
b=(n*(n+1)*(2*n+1))/6;
if(n!=c)
b=(c*(c+1)*(2*c+1))/6-((c*(c+1)/2)-a)*c;
printf("%d %d\n",a,b);
}
return 0;
}
posted on 2010-08-02 23:36
heiseon 阅读(257)
评论(0) 编辑 收藏 引用