POJ1019——Number Sequence(大数处理)
Number Sequence
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
题目大意:
给定一个字符串,构成如下:
1121231234...123456789101112...12345678910111213..N
问字符串的第i位是多少。
解题思路:
将字符串划分成N段。
1 12 123 1234 ... 1234567891011 ... 123456789101112....N
那么对于第K段的长度len[k]=len[k-1]+K这个数字的长度。即len[k]=len[k-1]+log10(k)+1。
再定义sum[k]=sum[k-1]+len[k]。通过比较sum[]与i的大小即可定位到i所在的字段。
假设i在第k个字段(1234567...t....k)中,那么i-sum[k-1]表示的就是i在第k个字段中的位置。
再通过公式log10(j)+1就能判断出i所在的t和在t中的位置pos。
ans=t/pow(10,pos)%10。
Code:
/*************************************************************************
> File Name: poj1019.cpp
> Author: Enumz
> Mail: 369372123@qq.com
> Created Time: 2014年11月08日 星期六 02时33分13秒
************************************************************************/ #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<list>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include<cmath>
#include<bitset>
#include<climits>
#define MAXN 40000
using namespace std;
long long len[MAXN],sum[MAXN];
void init()
{
for (int i=;i<MAXN;i++)
{
len[i]=len[i-]+(int)log10((double)i)+;
sum[i]=sum[i-]+len[i];
}
}
int solve(int N)
{
int i=;
while (sum[i]<N)
i++;
N-=sum[i-];
int len_k=,t;
for(t=;len_k<N;t++)
len_k+=(int)log10((double)t)+;
int pos=len_k-N;
return (t-)/(int)pow((double),pos)%;
}
int main()
{
int T;
cin>>T;
int N;
init();
while (T--)
{
cin>>N;
cout<<solve(N)<<endl;
}
return ;
}
POJ1019——Number Sequence(大数处理)的更多相关文章
- POJ1019 Number Sequence
Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 36256 Accepted: 10461 ...
- HDU 1005 Number Sequence
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- POJ 1019 Number Sequence
找规律,先找属于第几个循环,再找属于第几个数的第几位...... Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submi ...
- HDOJ 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- Number Sequence
Number Sequence A number sequence is defined as follows: f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) ...
- [AX]AX2012 Number sequence framework :(三)再谈Number sequence
AX2012的number sequence framework中引入了两个Scope和segment两个概念,它们的具体作用从下面序列的例子说起. 法国/中国的法律要求财务凭证的Journal nu ...
- KMP - HDU 1711 Number Sequence
Number Sequence Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 1005:Number Sequence(水题)
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- Number Sequence 分类: HDU 2015-06-19 20:54 10人阅读 评论(0) 收藏
Number Sequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...
随机推荐
- 仿照CREATE_FUNC实现CCLayer中的返回CCScene* 的静态函数,宏包装成CREATE_SCENE(XXLayer)
#define CREATE_SCENE(__TYPE__)\ CCScene *scene()\ { CCScene *scene=CCScene::create();\ __TYPE__ *lay ...
- java 参数化类型
package com.gxf.collection; import java.util.LinkedList; public class TestForT<T> { private Li ...
- CoffeeRobotTeam项目组报告
一.小组分工 模块 任务 责任人 备注 报告 需求分析 熊振威 功能分析 熊振威 项目报告 熊振威 人机界面 秦勤.洪超 单元测试 姜进.张文强 机器人代码 机器人类 徐意.余拥军.孙智博 机器人运动 ...
- java并发编程:进程和线程
java并发编程涉及到很多内容,当然也包括多线程,再次补充点相关概念 原文地址:http://www.cnblogs.com/dolphin0520/p/3910667.html 一.操作系统中为什么 ...
- Android Studio 单刷《第一行代码》系列 02 —— 日志工具 LogCat
前情提要(Previously) 本系列将使用 Android Studio 将<第一行代码>(书中讲解案例使用Eclipse)刷一遍,旨在为想入坑 Android 开发,并选择 Andr ...
- IIS8中 出现ashx 401:未授权,uploadify上传文件失败
环境:阿里云服务器 windows2012 + IIS8 +asp.net 访问IIS 出现能正常访问aspx页面,但是通过ajax访问ashx上传文件的时候就出现ashx Status Code ...
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) C - Bear and Colors
题目链接: http://codeforces.com/contest/673/problem/C 题解: 枚举所有的区间,维护一下每种颜色出现的次数,记录一下出现最多且最小的就可以了. 暴力n*n. ...
- 了解javascript中的事件(二)
本文目录如下: 零.寒暄 一.事件的分类 二.事件代理 2.1 问题引出 2.2 什么是事件代理 2.3 完整示例 二.事件代理 三.事件代理思想的用处 四.总结 零.寒暄 这篇博客本该出现在两个月以 ...
- c++ assert
#include<iostream> #include <assert.h> using namespace std; int main() { ; assert(a == ) ...
- socket网络编程中read与recv区别
socket网络编程中read与recv区别 1.read 与 recv 区别 read 原则: 数据在不超过指定的长度的时候有多少读多少,没有数据则会一直等待.所以一般情况下:我们读取数据都需要采用 ...