lightoj 1234
2016-03-03 19:56
288 查看
Description
In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers:
In this problem, you are given n, you have to find Hn.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 108).
Output
For each case, print the case number and the nth harmonic number. Errors less than 10-8 will be ignored.
Sample Input
12
1
2
3
4
5
6
7
8
9
90000000
99999999
100000000
Sample Output
Case 1: 1
Case 2: 1.5
Case 3: 1.8333333333
Case 4: 2.0833333333
Case 5: 2.2833333333
Case 6: 2.450
Case 7: 2.5928571429
Case 8: 2.7178571429
Case 9: 2.8289682540
Case 10: 18.8925358988
Case 11: 18.9978964039
Case 12: 18.9978964139
想事先打表、然后输出的,发现空间爆了,坑
思路:每50个单位长度的区间统计一下和,对每次输入的n/50 ,多出的部分就直接一个一个算
In mathematics, the nth harmonic number is the sum of the reciprocals of the first n natural numbers:
In this problem, you are given n, you have to find Hn.
Input
Input starts with an integer T (≤ 10000), denoting the number of test cases.
Each case starts with a line containing an integer n (1 ≤ n ≤ 108).
Output
For each case, print the case number and the nth harmonic number. Errors less than 10-8 will be ignored.
Sample Input
12
1
2
3
4
5
6
7
8
9
90000000
99999999
100000000
Sample Output
Case 1: 1
Case 2: 1.5
Case 3: 1.8333333333
Case 4: 2.0833333333
Case 5: 2.2833333333
Case 6: 2.450
Case 7: 2.5928571429
Case 8: 2.7178571429
Case 9: 2.8289682540
Case 10: 18.8925358988
Case 11: 18.9978964039
Case 12: 18.9978964139
想事先打表、然后输出的,发现空间爆了,坑
思路:每50个单位长度的区间统计一下和,对每次输入的n/50 ,多出的部分就直接一个一个算
#include<cstdio> #include<cmath> double num[100000009/50]; int main() { num[0]=0; int ans=1; for(int i=1; i<=(1e8+50) / 50;++i){ double a=0; for(int j=(i-1)*50+1;j<=i*50;j++) //每单位长度为50的区间统计一次总和 a+=(1.0/j); num[i]=num[i-1]+a; } int n,t=0; scanf("%d",&n); while(n--){ int a; scanf("%d",&a); int b=a/50; double fin=num[b]; for(int i=b*50+1;i<=a;++i) //多出来的的再来单个单个算、 fin+=1.0/i; printf("Case %d: %.10lf\n",++t,fin); } }
相关文章推荐
- android studio sdk 配置
- mysql 支持的存储引擎(极简版)
- 2015年大二上-数据结构-查找-1-(2)-分块查找
- git常见命令& gerrit操作方法
- 学习记录
- ROS消息发布器与接收器(C++)注意事项
- npm配置镜像、设置代理
- SQLSERVER 链接 MYSQL 的 两种方法 及 未发现数据源名称并且未指定默认驱动程序 处理办法
- Android中的layout_width和width的区别
- jquery插入元素
- 方法覆盖(Override)中的一些问题 1
- 1044. 火星数字(20)
- 根据文本生成位图
- SFTP 实践
- Java获取、删除文件和目录
- gradle多渠道打包及友盟统计-eclipse版本
- 多种非接触卡 ATQA 字节说明
- Class Of Marquee Scroll通用不间断滚动JS脚本
- HDOJ-2057(A + B Again)
- 旋转字符串的三种算法