题目地址:http://poj.org/problem?id=1019

Number Sequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 35680   Accepted: 10287

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
分析:序列如上所示,要求这个序列的第n位是什么,首先需要知道的是:一个数x的宽度怎么算?可以每次让x除10,看看多少次可以除尽。
不过比较麻烦,有简单的算法:f[x]=log10(x)+1; 999的位宽=log10(999)+1=3;
有了这个就可以来想怎么解决上面的问题。
参考博客1:http://www.cnblogs.com/ACShiryu/archive/2011/08/05/2129009.html
参考博客2:http://blog.csdn.net/lyy289065406/article/details/6648504一个大序列可以划分成许多个有规律的子序列,先找到第n位在那个子序列上,再找到在子序列的那个数字上,再找到在该数字的哪一位上。
代码:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>
#include <cmath>
#include <iostream>
#include <string>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define N 100000+100 using namespace std;
long long int f[40000];
long long int s[40000];
int main()
{
int i, j; memset(f, 0, sizeof(f));
memset(s, 0, sizeof(s)); long long int sum=0;
f[1]=1; s[1]=1;
i=2;
while( i<=31269 )
{
f[i]=f[i-1]+(int)log10((double)i)+1;
s[i]=s[i-1]+f[i];
i++;
}//打表 int tg; scanf("%d", &tg);
while(tg--){
int n;
scanf("%d", &n);
for(i=1; i<=31269; i++){
if(s[i]>=n) break;
}
int pos=i; //找到在第pos个子序列上
n=n-s[pos-1];
for(j=1; j<=pos; j++){
n=n-((int)log10((double)j)+1); //减去当前这个数的宽度
if(n<=0){
break;
}
}
int pos2=j;//找到在子序列的第pos2个数上
n=n+(int)log10((double)pos2)+1;//在这个数的第n位上 int dd=(int)log10((double)pos2)+1-n;
int q=1, w=1;
for(i=1; i<=dd; i++) {q=q*10; w=w*10;} w=w*10; int ans;
ans=pos2%w/q;
printf("%d\n", ans );//输出结果
}
return 0;
}
												

poj 1019 Number Sequence 【组合数学+数字x的位宽函数】的更多相关文章

  1. Poj 1019 Number Sequence( 数据分析和操作)

    一.题目大意 有这样一个序列包含S1,S2,S3...SK,每一个Si包括整数1到 i.求在这个序列中给定的整数n为下标的数. 例如,前80位为1121231234123451234561234567 ...

  2. POJ 1019 Number Sequence

    找规律,先找属于第几个循环,再找属于第几个数的第几位...... Number Sequence Time Limit: 1000MS Memory Limit: 10000K Total Submi ...

  3. POJ - 1019 Number Sequence (思维)

    https://vjudge.net/problem/POJ-1019 题意 给一串1 12 123 1234 12345 123456 1234567 12345678 123456789 1234 ...

  4. POJ 1019 Number Sequence 解读

    这是一个看似简单,其实很难受. 本来我想发挥它的标题轨道基础.没想到反被消遣-_-|||. 看它在个人基础上,良好的数学就干脆点,但由于过于频繁,需求将被纳入全,因此,应该难度4星以上. 方法就是直接 ...

  5. PKU 1019 Number Sequence(模拟,思维)

    题目 以下思路参考自discuss:http://poj.org/showmessage?message_id=176353 /*我的思路: 1.将长串数分成一个个部分,每个部分是从1到x的无重复的数 ...

  6. POJ 1019:Number Sequence 二分查找

    Number Sequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36013   Accepted: 10409 ...

  7. Number Sequence(poj 1019)

    题意: 有一串数字串,其规律为 1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678910 1234567891011 1234 ...

  8. HDU 1005 Number Sequence

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  9. hdu 1005:Number Sequence(水题)

    Number Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

随机推荐

  1. cdn日志统一下载程序

    最近实现了网宿cdn,阿里云cdn,腾讯cdn的日志统一格式下载程序,使用简单方便,有需要详见代码: https://github.com/mikeluwen/CdnLogDownload

  2. 二分Kmeans的java实现

    刚刚研究了Kmeans.Kmeans是一种十分简单的聚类算法.可是他十分依赖于用户最初给定的k值.它无法发现随意形状和大小的簇.最适合于发现球状簇.他的时间复杂度为O(tkn).kmeans算法有两个 ...

  3. NFS详细分析

    1. NFS服务介绍 1.1什么是NFS服务 NFS(Network File System)即网络文件系统,它允许网络中的计算机之间通过TCP/IP网络共享资源.在NFS的应用中,本地NFS的客户端 ...

  4. Spring Boot从入门到实战:整合通用Mapper简化单表操作

    数据库访问是web应用必不可少的部分.现今最常用的数据库ORM框架有Hibernate与Mybatis,Hibernate貌似在传统IT企业用的较多,而Mybatis则在互联网企业应用较多.通用Map ...

  5. webpack 功能大全 【环境配置】

    1. webpack简介 webpack 是一个模块打包工具.它使得模块相互依赖并且可构建等价于这些模块的静态资源.相比于已经存在的模块打包器(module bundler),webpack的开发动机 ...

  6. 获取maven下resouce中的文件为File对象

    例如src/main/resources目录下面有个config/interfaces/quick-stock.xml文件我想直接在某一个Java类里面获取到这个文件的File对象,怎么整?(注意噢: ...

  7. mysql数据索引

    索引是建立在数据库表中的某些列的上面.因此,在创建索引的时候,应该仔细考虑在哪些列上可以创建索引,在哪些列上不能创建索引.一般来说,应该在这些列上创建索引,例如:在经常需要搜索的列上,可以加快搜索的速 ...

  8. 怎样解决mysql最后一步提示未响应

    1.在开始菜单下,点击运行,输入regedit,进入注册表编辑器目录下 2.在注册表编辑器里system下找到controlset001,controlset002,currentcontrolset ...

  9. python获取shell输出(转)

    From:http://www.cnblogs.com/snow-backup/p/5035792.html   python中获取shell命令输出的方法: 1.  import subproces ...

  10. 基于react-native android的新闻app的开发

    使用平台:android 代码获取地址:https://github.com/wuwanyu/ReactNative-Android-MovieDemo 项目展示: 结构图: SpalashScree ...