POJ 1019 Number Sequence(水~)
2015-12-28 14:22
465 查看
Description
一个数字序列,规律是这样的:
1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910 1234567891011 123456789101112……
求这个序列中的第n位是什么数字
Input
第一行为一个整数T表示用例组数,每组用例占一行为一个整数n
Output
对于每组用例,输出这个数字序列中第n位的数字
Sample Input
2
8
3
Sample Output
2
2
Solution
简单题,首先统计每个数字占的位数cnt[i]=(int)log10(i)+1,然后统计每个1~i组成的串占的位数sum[i]=cnt[1]+…+cnt[i],预处理出这些之后,对于每个n,首先通过不断减sum[i]得知第n位处于哪一个1~m之间,然后再不断减cnt[i]得知第n位位于1~m中哪一个数中,然后问题就变成找到一个数字temp的第n位,这个可以先将从左往右数的第n位转化为从右往左数的第(int)log10(temp*1.0)+1-n位,那么ans=temp/(int)pow(10.0,1.0*n)%10
Code
一个数字序列,规律是这样的:
1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910 1234567891011 123456789101112……
求这个序列中的第n位是什么数字
Input
第一行为一个整数T表示用例组数,每组用例占一行为一个整数n
Output
对于每组用例,输出这个数字序列中第n位的数字
Sample Input
2
8
3
Sample Output
2
2
Solution
简单题,首先统计每个数字占的位数cnt[i]=(int)log10(i)+1,然后统计每个1~i组成的串占的位数sum[i]=cnt[1]+…+cnt[i],预处理出这些之后,对于每个n,首先通过不断减sum[i]得知第n位处于哪一个1~m之间,然后再不断减cnt[i]得知第n位位于1~m中哪一个数中,然后问题就变成找到一个数字temp的第n位,这个可以先将从左往右数的第n位转化为从右往左数的第(int)log10(temp*1.0)+1-n位,那么ans=temp/(int)pow(10.0,1.0*n)%10
Code
#include<stdio.h> #include<string.h> #include<math.h> #define maxn 33333 typedef long long ll; ll cnt[maxn],num[maxn],n; int t,ans; void init() { memset(cnt,0,sizeof(cnt)); memset(num,0,sizeof(num)); for(int i=1;i<maxn;i++) { cnt[i]=cnt[i-1]+(int)log10(1.0*i)+1; num[i]=num[i-1]+cnt[i]; } } int main() { init(); scanf("%d",&t); while(t--) { scanf("%lld",&n); int temp=1; while(n>num[temp])temp++; n-=num[temp-1]; temp=1; while(n>(int)log10(temp*1.0)+1)n-=((int)log10(temp*1.0)+1),temp++; n=(int)log10(temp*1.0)+1-n; ans=temp/(int)pow(10.0,1.0*n)%10; printf("%d\n",ans); } return 0; }
相关文章推荐
- easyui数据选择后重新定位到第一行
- AFNetWorking 关于manager.requestSerializer.timeoutInterval 不起作用的问题
- 猫猫学iOS之UITextField右边设置图片,以及UITextField全解
- UINavigationItem UINavigationBar 关系分析
- UGUI中RectTRanform笔记
- iOS开发日记56-详解UIImage
- arduino中断和一些问题
- NGUI使用教程(3) 使用外部图片制作Atlas(图集)
- request.getParameter(“参数名”) 中文乱码解决方法
- 提示框的使用->UIAlertController类的使用
- easyui整理资料
- 对easyui datagrid进行扩展,当滚动条拉直最下面就异步加载数据
- Android开发如何在getContentResolver().query()方法中实现结果去重
- easyui自定义扩展视图实现功能--------datagrid返回记录为0时显示“没有记录”
- iOS错误提示:-[UITextSelectionView chatTextViewTextChange]: unrecognized selector sent to instance 0x7f
- BZOJ 4302(Hdu 5301 Buildings-构造)
- Using platform encoding (GBK actually) to copy filtered resources, i.e. build is platform dependent!
- 【Codeforces】280D k-Maximum Subsequence Sum【区间内k段最大和——线段树模拟费用流】
- 数据库连接池Druid
- 简析Handler、MessageQueue、Looper