POJ1019 Number Sequence
2015-10-22 19:19
399 查看
Number Sequence
For example, the first 80 digits of the sequence are as follows:
11212312341234512345612345671234567812345678912345678910123456789101112345678910
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 36256 | Accepted: 10461 |
Description
A single positive integer i is given. Write a program to find the digit located in the position i in the sequence of number groups S1S2...Sk. Each group Sk consists of a sequence of positive integer numbers ranging from 1 to k, written one after another.For example, the first 80 digits of the sequence are as follows:
11212312341234512345612345671234567812345678912345678910123456789101112345678910
Input
The first line of the input file contains a single integer t (1 ≤ t ≤ 10), the number of test cases, followed by one line for each test case. The line for a test case contains the single integer i (1 ≤ i ≤ 2147483647)Output
There should be one output line per test case containing the digit located in the position i.Sample Input
2 8 3
Sample Output
2 2
Source
Tehran 2002, First Iran Nationwide Internet Programming Contest 题意就不说了,看下就明白。思路就是打表,然后就是要知道求一个数的位数:(int)log10((double)x)+1/* ID: LinKArftc PROG: 1019.cpp LANG: C++ */ #include <map> #include <set> #include <cmath> #include <stack> #include <queue> #include <vector> #include <cstdio> #include <string> #include <utility> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #define eps 1e-8 #define randin srand((unsigned int)time(NULL)) #define input freopen("input.txt","r",stdin) #define debug(s) cout << "s = " << s << endl; #define outstars cout << "*************" << endl; const double PI = acos(-1.0); const int inf = 0x3f3f3f3f; const int INF = 0x7fffffff; typedef long long ll; const int maxn = 40000; ll sum[maxn], line[maxn];//分别是前i行的位数和,第i行的位数 int getbit(int x) { return (int)log10((double)x) + 1; } void init() { sum[1] = line[1] = 1; for (int i = 2; i <= 35000; i ++) { line[i] = line[i-1] + getbit(i); sum[i] = sum[i-1] + line[i]; } } int getpos(int x, int pos) { int len = getbit(x); for (int i = 1; i <= len - pos; i ++) x /= 10; return x % 10; } int main() { init(); int T; ll n; scanf("%d", &T); while (T --) { scanf("%lld", &n); int cur = 1; while (sum[cur] < n) cur ++; ll pos = n - sum[cur-1]; while (cur >= 1) { if (pos > line[cur-1]) { pos -= line[cur-1]; printf("%d\n", getpos(cur, pos)); break; } else cur --; } } return 0; }
相关文章推荐
- zoj 3911 Prime Query(线段树)
- duilib 自定义控件TeamList 继承与CListUI
- duilib 自定义控件1 CTileLayoutUI
- 解决VirtualBox分辨率太小及VBoxGuestAdditions_4.3.12.iso下载地址
- valueForKeyPath 的使用(真心不知道啊)
- make: *** /lib/modules/2.6.32-573.7.1.el6.x86_64/build/: No such file or directory. Stop.
- HDU 5504 GT and sequence(贪心)
- Android_UiAutomator(安卓UI自动化)环境搭建
- 自定义UIPageControl
- CompileSdkVersion、buildToolVersion、minSdkVersion、targetSdkVersion、maxSdkVersion
- 《iOS总结》UITableViewCell的增加与删除-MickyChiang
- 基于zookeeper的分布式Queue
- UILabel设置多种字体、颜色 (NSMutableAttributedString)
- UINavigationController
- iOS SDK基础知识:UITextView & UITextViewDelegate
- 用UGUI实现小地图MiniMap
- requestFeature() must be called before adding content错误处理
- hdu2767 Proving Equivalences Tarjan缩点
- Hibernate中query的常用方法
- UIPanGestureRecognizer上下左右滑动方向判断算法