POJ1019 Number Sequence
2024-09-26 08:14:28
Number Sequence
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
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 = ; ll sum[maxn], line[maxn];//分别是前i行的位数和,第i行的位数 int getbit(int x) {
return (int)log10((double)x) + ;
} void init() {
sum[] = line[] = ;
for (int i = ; i <= ; i ++) {
line[i] = line[i-] + getbit(i);
sum[i] = sum[i-] + line[i];
}
} int getpos(int x, int pos) {
int len = getbit(x);
for (int i = ; i <= len - pos; i ++) x /= ;
return x % ;
} int main() { init();
int T;
ll n;
scanf("%d", &T);
while (T --) {
scanf("%lld", &n);
int cur = ;
while (sum[cur] < n) cur ++;
ll pos = n - sum[cur-];
while (cur >= ) {
if (pos > line[cur-]) {
pos -= line[cur-];
printf("%d\n", getpos(cur, pos));
break;
} else cur --;
}
} return ;
}
最新文章
- Amazon Interview | Set 27
- docker学习(7) docker-compose使用示例
- Range Sum Query - Immutable
- 【Unity3D】计算二维向量夹角(-180到180)
- Raspberry Pi 3 Model B 安装 OSMC
- two day python基础知识
- java List 排序 Collections.sort() 对 List 排序
- Java调第三方的webservice接口
- java面板
- Unity3d Shader开发(四)UsePass ,GrabPass ,SubShader Tags
- 动态规划——C编辑最短距离
- Spring 学习笔记01
- Unity sqlite学习笔记一
- python3 selenium 登录操作
- Velocity(7)——velocity进阶用法
- 异常处理--logging模块
- 【转】一文掌握 Linux 性能分析之内存篇
- js对json解析获取对应属性的值,JSON.stringify()和JSON.parse()
- git回答整理
- Android实现动态改变屏幕方向(Landscape &; Portrait)