一些项目——Heritage from father

Problem Description

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.

Input

The input will consist of some cases,each case takes a line with only one integer N(0<N<2^31).It ends with a single 0.

Output

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

Sample Input

1
3
0

Sample Output

1.00E0
1.00E1

Hint

Hint

when N=1 ,There is 1 gold coins.

when N=3 ,There is 1+3+6=10 gold coins.

代码

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
    double sum,n,a,m;
    while(cin>>n&&n!=0)
    {
        sum=n*(n+1)/6.0*(n+2);
        a=log10(sum);
        m=sum/pow(10,int(a));
        cout<<setiosflags(ios::fixed)<<setprecision(2)<<m<<'E'<<int(a)<<endl;
    }
    return 0;
}

sum算出i*(i+1)/2之和

a计算位数

m代表和除以位数,即将总和sum成为一位数

时间: 2024-10-07 00:10:29

一些项目——Heritage from father的相关文章

HDU Heritage from father

Heritage from father Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131070/65535K (Java/Other) Total Submission(s) : 71   Accepted Submission(s) : 18 Font: Times New Roman | Verdana | Georgia Font Size: ← → Problem Description Famous Harry Po

hdu 1178 Heritage from father (推导)

题意: 有一个金币堆的金字塔,最上层就有一个金币,以后的i层都是边长为i的实心三角形,给你层数,问:一共有多少个金币?(用科学计数法表示,并且保留两位小数) 解题思路: 根据题意可知求出1*n+2*(n-1)+3*(n-2)+4*(n-3)+.......+(n-2)*3+(n-1)*2+n*1的和即可,但是要化简一下求出n*(n+1)*(n+2)/6; 证明: 1 * n + 2 (n - 1) + 3 (n - 2) + …… + (n - 2) * 3 + (n - 1) * 2 + n

hdoj-1178-Heritage from father【科学计数法表示】

Heritage from father Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Submission(s): 6076 Accepted Submission(s): 2214 Problem Description Famous Harry Potter,who seemd to be a normal and poor boy,is actually a

[hiho 13]最近公共祖先 一

题目描述 由于这个跟后几周是一个系列,这周的做法比较简单. 把第一个人的所有祖先做标记,第二个人向上查找祖先直到找到一个标记过的节点或无法继续为止. 代码其实没什么意思. import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Main { private static class Person { String name; String father; boolean tag

RuPengWang项目

项目 Day1------------------------- 说明:建外键约束.ashx+Razor RupengWang创建三个类库Model DAL BLL后台:RupengWang.Admin 前台:RupengWang.Front 建一个后台管理员的表T_AdminUsers(Id UserName Password)打开动软,生成三层代码 (报错,是因为UAC是不用随意的写入数据,方法是以管理员运行,没有就去属性兼容性中找),(工具-选项-配置-命令规则),(新建net项目-工厂模

Javascript 优化项目代码技巧之语言基础(一)

1.全局变量污染与变量提升2.数据类型3.特殊值(NaN.undefined.null)4. === 与 ==5.没有真正的数组6.避免使用with与eval7.消除switch歧义8.不要省略块标志 { } Javascript的弱类型以及函数作用域等规则使用编写Javascript代码极为容易,但是编写可维护.高质量的代码却变得十分困难,这个系列的文章将总结在项目开发过程中,能够改善代码可读性.可维护性及优化运行性能的一系列技巧.     如有问题,请不吝指出,非常感谢:如果喜欢,右下角点个

项目中遇到的进程间通信方式

1)socket:经常遇到,不讲了 2)信号:使用kill发送信号,signal,settimer等系统调用都能对另一个进程发送信号,达到了进程间通信的目的. kill(p1,16);    /*向进程号为p1的进程 发中断信号16*/ signal(SIGINT,go); /*接收到SIGINT信号后,转go函数去处理它*/ 3)共享内存:使用mmap系统调用能够做到共享内存,mmap的使用方式是以fd为入参,两个进程都打开一个文件名,并用mmap将这个fd映射到各自的进程环境,使用mmap反

GEF(Graphical Editor Framework) Eclipse项目入门系列(3)---Draw2D例子演示

在"GEF(Graphical Editor Framework) Eclipse项目入门系列(2)---Draw2D开发环境的搭建"一文中,我给大家介绍了Draw2D的开发环境的搭建.下一步,根据软件行业的惯例,需要展示一个例子,这样大家才更有兴趣去学习和探索这门技术.好了,废话少说,作者就借花献佛,用Dan Rubel,Jaimen Wren和Eric Clayberg的一个例子Draw2D的例子和大家分享一下.这个例子包括两个类,GenealogyView和FigureMover

JavaWeb项目笔记包括jsp的用法selevt,HTML5

-------------------<认识HTML5>----------------- 1 <>生成的快捷键 是table键 ctrl+D是直接复制一行 2 <h1>-<h6> 标题 字体由大到小 <p> 段落 <br> 换行标签 <hr> 水平线标签 <em> 斜体 <strong> 字体加粗 ../ 返回上一级目录 > 大于号 < 小于号   空格 " 引号"