HDU ACM 1178 Heritage from father
Heritage from father
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Submission(s): 3827 Accepted Submission(s): 1350
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
when N=1 ,There is 1 gold coins. when N=3 ,There is 1+3+6=10 gold coins.
Hint
HintSource
Recommend
JGShining
#include<stdio.h> #include<math.h> int main() { int n, m; double sum, result; while(scanf("%d",&n)== 1 && n) { sum = 1.0 * n * (n+1) * (n+2) / 6; m = (int)log10(sum); result = sum/pow(10,m); printf("%.2lfE%d\n", result, m); } return 0; }
解题报告:
怎么说呢?承认自己的无知和错误为什么是件很难做到的事情!
插入自己的源代码,unsigned long long 的取值范围为:0~18446744073709551615,即0~(2^64 -1)大概为10的19次方,相比之下,double的取值范围为:0以及2.3*10^-308~1.7*10^308完全足够存储2147483647(2^31-1)的3次方,所以用unsigned long long 远远不够 !
从这题中能够学到的东西是:1> 第一次用sprintf;2>学会了用math函数中的pow和log,3>温习了科学计数法的输出格式;从人本身这个角度来看,自己出现的问题仍然存在,一是不懂得如何分析出现错误的情况从而容易焦急且无从下手,情绪化不知道哪里是Point导致滞留在同一问题上从而浪费大量时间;二是不懂得事后总结,比如说所花的时间值不值得,遇见问题时如何抉择才能使损失的最小;三是做题少,书看得少,阅历和经验不够,这也是最重要和最需解决的。

更多内容请关注个人微信公众号 物役记 (微信号:materialchains)
作者:雪影蓝枫
本文版权归作者和博客园共有,欢迎转载,未经作者同意须保留此段声明,且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?