程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> POJ1019:Number Sequence(組合計數)

POJ1019:Number Sequence(組合計數)

編輯:C++入門知識

Number Sequence
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 28810   Accepted: 8003
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. www.2cto.com
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
MYCode:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
typedef long long LL;
#define MAX 40010
LL sum[MAX];
LL len[MAX];
int func(int x)
{
    int ct=0;
    while(x>=1)
    {
        ct++;
        x/=10;
    }
    return ct;
}
void cal()
{
    int x;
    for(x=1;x<=9;x++)
    len[x]=x;
    int i;
    for(x=10;x<=40000;x++)
    {
        int d=func(x);
        int tot=0;
        for(i=1;i<d;i++)
        {
            int num=(int)(pow(10.0,i)-pow(10.0,i-1));
            tot+=i*num;
        }
        int add=x-(int)pow(10.0,d-1)+1;
        tot+=add*d;
        len[x]=tot;
    }
    sum[0]=0;
    for(i=1;i<=40000;i++)
    {
        sum[i]=sum[i-1]+len[i];
    }
}
int b_search(int k)
{
    int lt=1,rt=40000;
    while(lt<rt)
    {
        int mid=(lt+rt)/2;
        if(sum[mid]<k)
        lt=mid+1;
        else
        rt=mid;
    }
    return rt;
}
int find(int r,int x)
{
    int lt=1,rt=x;
    while(lt<rt)
    {
        int mid=(lt+rt)/2;
        if(len[mid]<r)
        lt=mid+1;
        else
        rt=mid;
    }
    int add=r-len[rt-1];
    int st[20];
    int ct=0;
    while(rt>=1)
    {
        st[ct++]=rt%10;
        rt/=10;
    }
    return st[ct-add];
}
int solve(int k)
{
    int x=b_search(k);
    int r=k-sum[x-1];
    return find(r,x);
}
int main()
{
    int k;
    cal();
    int i;
    int t;
    //cout<<sum[40000]<<endl;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&k);
        int ans=solve(k);
        printf("%d\n",ans);
    }
}
//16MS
len[i]:表示以從1開始,以i結尾的序列的長度
sum[i]:表示以1,2,3,...,i結尾的序列的長度之和
經檢查,sum[40000]已經覆蓋題目的數據范圍.
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved