题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1178

题目描述

Famous Harry Potter,who seemd to be a normal and poor boy,is actually a wizard.Everything changed 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 a lot 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.Harry was astonishing ,coz there were piles of gold coins.

The way of packing these coins by Goblins was really special.Only one coin was on the top,and three coins consisted an triangle were on the next lower layer.The third layer has six coins which were also consisted an triangle,and so on.On the ith layer there was an triangle have i coins each edge(totally $i*(i+1)/2$).The whole heap seemed just like a pyramid.Goblin still knew the total num of the layers,so it’s up you to help Harry to figure out the sum of all the coins.

思路

这题有点坑,处理指数处。

首先求出求和公式,每一层$i$金币数为$i*(i+1)/2 = (i*i+i)/2$

已知
$$ 1+2+3+...+n = (n+1)*n/2\\ 1*1 + 2*2 + ... + n*n = n(n+1)(2n+1)/6 $$

推出求和公式
$$ sum = n(n+1)(n+2)/6;$$

sum比较大,要分解成科学计数法,先计算sum的长度,

长度可以这样算 $sum = x * 10^e => e = log_{10}(sum);$

最后按要求输出。

$sum / 10^e$即为第一位数字,e为指数部分。

AC代码

/*************************************************************************
    > File Name: 1178.cpp
      > Author: Netcan
      > Blog: http://www.netcan.xyz
      > Mail: 1469709759@qq.com
      > Created Time: Mon 11 May 2015 06:03:51 PM CST
 ************************************************************************/

#include <iostream>
#include <vector>
#include <string>
#include <queue>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <sstream>
#include <deque>
#include <functional>
#include <iterator>
#include <list>
#include <map>
#include <memory>
#include <stack>
#include <set>
#include <numeric>
#include <utility>
#include <cstring>
using namespace std;

int main()
{
    double N;
    while(cin >> N && N) {
        int e;
        double ans = (N*(N+1)*(N+2))/6; // 求和
        e = log(ans) / log(10); // 提取长度
        printf("%.2fE%d\n", (double)ans*1.0/(pow(10, e)), e);
    }
    return 0;
}