hdu 1597 find the nth digit(数学题)
来源:互联网 发布:大唐电信数据所招聘 编辑:程序博客网 时间:2024/06/05 18:21
原题链接:
http://acm.hdu.edu.cn/showproblem.php?pid=1597
方法1:直接循环找到所在那两个数之间。
方法2:求根公式。
x^2+x-2*n>=0(x为Sx的下标,n为第n个数)
求根公式得(sqrt(8*n+1)-1)/2 ,另一情况为负数舍去。
关键:求出这个double类型,应该满足>=0,所以向上取整。
注意:1<=n<(2^31),所以用unsigned int或者long long
代码如下:
方法1:
#include<iostream>using namespace std;int main(){int T;cin >> T;while (T--){int n;cin >> n;int sum = 0;int k = 1;while (sum < n){sum += k;k++;}int temp=sum - k+1;int ans = (n - temp) % 9 ? (n - temp) % 9 : 9;cout << ans << endl;}return 0;}
方法2:
#include<iostream>#include<cmath>#include<cstdio>using namespace std;int main(){int T;scanf("%d", &T);while (T--){long long int n;scanf("%d", &n);long long int k=ceil((sqrt(8.0 * n + 1) - 1) / 2.0);long long int temp = k* (k - 1) / 2;int ans = (n - temp) % 9 ? (n - temp) % 9 : 9;cout << ans << endl;}return 0;}
0 0
- hdu 1597 数学题 find the nth digit
- hdu 1597 find the nth digit(数学题)
- HDU 1597 find the nth digit (数学题)
- hdu Problem - 1597 find the nth digit
- HDU 1597 find the nth digit
- hdu 1597 find the nth digit
- Hdu 1597 find the nth digit
- hdu 1597 find the nth digit
- hdu 1597 find the nth digit
- HDU 1597 find the nth digit
- HDU 1597:find the nth digit
- hdu 1597 find the nth digit
- hdu find the nth digit(1597)
- hdu 1597 find the nth digit
- HDU 1597 find the nth digit
- HDU 1597 find the nth digit
- HDU 1597 find the nth digit 水题
- HDU 1597:find the nth digit【规律】
- Android问题集(三)——禁止进入新Activity界面后因Edittext而弹出输入法;禁止横屏效果
- HDU 2871 Memory Control
- ssh 远程执行脚本
- hdu 2829 Lawrence 斜率优化
- POJ 1847 Tram
- hdu 1597 find the nth digit(数学题)
- *POJ 1201 - Intervals(查分约束)
- 【数据结构】-归并排序,基数排序
- Windows内核编程基础篇之文件操作(二)
- 测试
- 逻辑回归和SVM的区别
- 转:Java学习路线图,专为新手定制的Java学习计划建议
- 模态ViewController及单例
- 项目开发注意事项