HDU--1178--Heritage from father(求和公式)

Description

Famous HarryPotter,who seemd to be a normal and poor boy,is actually a wizard.Everythingchanged when he had his birthday of ten years old.A huge man called 'Hagrid'found Harry and lead him to a new world full of magic power. 
If you've read this story,you probably know that Harry's parents had left him alot of gold coins.Hagrid lead Harry to Gringotts(the bank hold up by Goblins).And they stepped into the room which stored the fortune from his father.Harrywas astonishing ,coz there were piles of gold coins. 
The way of packing these coins by Goblins was really special.Only one coin wason the top,and three coins consisted an triangle were on the next lowerlayer.The third layer has six coins which were also consisted an triangle,andso on.On the ith layer there was an triangle have i coins each edge(totallyi*(i+1)/2).The whole heap seemed just like a pyramid.Goblin still knew thetotal num of the layers,so it's up you to help Harry to figure out the sum ofall the coins. 

 

Input

The input willconsist of some cases,each case takes a line with only one integerN(0

 

Output

对于每个输入的N,输出一行,采用科学记数法来计算金币的总数(保留三位有效数字

 

Sample Input

 1

3

0

 

Sample Output

 1.00E0

1.00E1

 

这里要用到一个求和公式

1*2+2*3+3*4+...n*(n+1)
=1(1+1)+2(2+1)+3(3+1)+···+n(n+1)
=1²+1+2²+2+3²+3+····+n²+n
=(1+2+3+····+n)+(1²+2²+3²+···n²)
=(1+n)n/2+n(n+1)(2n+1)/6
=n(n+1)/2[1+(2n+1)/3]
=n(n+1)(n+2)/3

而且科学计数法输出也要自己控制,电脑不能自己输出那种格式

#include 
#include 
int main()
{
	long long n,i;
	double s;
	while(scanf("%lld",&n)&&n)
	{
		s=n*((n+1)/6.0)*(n+2);			//要先算(n+1)/6.0,因为不这样的话数据可能会超
		int a=(int)log10(s);			//用取对数的方法求,a是log10(s)的整数部分,代表10的几次方,也就是E多少。
		double c=s/pow(10,(double)a);		//c是前缀
		printf("%0.2lfE%d\n",c,a);
	}	
}


你可能感兴趣的:(数学问题,技巧题)