博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ1019 Number Sequence
阅读量:4560 次
发布时间:2019-06-08

本文共 2295 字,大约阅读时间需要 7 分钟。

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

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

283

Sample Output

22

Source

, First Iran Nationwide Internet Programming Contest
 
题意就不说了,看下就明白。思路就是打表,然后就是要知道求一个数的位数:
(int)log10((double)x)+1
 
/*ID: LinKArftcPROG: 1019.cppLANG: C++*/#include #include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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;}

 

 
 

转载于:https://www.cnblogs.com/LinKArftc/p/4902512.html

你可能感兴趣的文章
VIP之Clipper
查看>>
vba处理excel
查看>>
oracle 面向對象(2)
查看>>
FreeRTOS随记
查看>>
实用小技巧:在键盘没有小键盘时怎么打开任务管理器
查看>>
我与计算机
查看>>
idea下载插件失败的问题
查看>>
powerdesigner相关概念理解
查看>>
小娜追踪快递
查看>>
hdu_1863_畅通工程_201403122000
查看>>
04_XML_01_入门基础
查看>>
top coder password题解
查看>>
extjs form textfield的隐藏方法
查看>>
编写.reg文件 导入注册表
查看>>
Linux常用命令
查看>>
关于Static、全局变量、局部变量、Extern之间的种种
查看>>
借助面向对象的思路开发多用户模拟类工具
查看>>
Android 实现真机远程调试并适应7寸屏大小
查看>>
在内部架设NuGet服务器
查看>>
百度地图SDk 使用
查看>>