Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

其实这道题对我这种菜鸟比较难,大神就别看了,代码如下。

Posted by jack_si at 2016-10-22 17:02:58 on Problem 1019
# include <cstdio>
# include <iostream>
const int R = 2147483647;
const int Q = 31500;
using namespace std;
int len[Q + 100];
int num[Q + 100];
long long sum[Q + 100];

void init()
{
    int i;
    for(i = 1; i < 10; i++)
        len[i] = 1;
    for(i = 10; i < 100; i++)
        len[i] = 2;
    for(i = 100; i < 1000; i++)
        len[i] = 3;
    for(i = 1000; i < 10000; i++)
        len[i] = 4;
    for(i = 10000; i < Q + 100; i++)
        len[i] = 5;
    num[1] = sum[1] = 1;
    for(i = 2; i < Q + 100; i++)
        {
            num[i] = num[i - 1] + len[i];
            sum[i] = sum[i - 1] + num[i];
            //cout << i << "  " << sum[i] << endl;
        }
}

int bsearch1(int key)
{
    int low, high;
    low = 1; high = Q;
    while(low <= high)
    {
        int middle = (low + high) >> 1;
        if(sum[middle] == key)
            return middle;
        if(sum[middle] < key)
            low = middle + 1;
        else
            high = middle - 1;
    }
    if(sum[low] <= key)
        return low;
    else
        return high;
}

int bsearch2(int key)
{
    int low, high;
    low = 1; high = Q;
    while(low <= high)
    {
        int middle = (low + high) >> 1;
        if(num[middle] == key)
            return middle;
        if(num[middle] < key)
            low = middle + 1;
        else
            high = middle - 1;
    }
    if(num[low] <= key)
        return low;
    else
        return high;
}

int main(void)
{
    init();
    int N ,j ,k ,cnt2, y;
    cin >> N;
    while(N--)
    {
        cnt2 = 0;
        cin >> k;
        int n = bsearch1(k);
        int _num = k - sum[n];
       // cout << n << "   " << _num << endl;
        if(_num == 0)
            cout << n % 10 << endl;
        else
        {
            j = bsearch2(_num);
            int _num1 = _num - num[j];
            int m = j + 1;
         //   cout << j << " " << _num1 << " " << m << endl;
            while(m)
            {
                m /= 10;
                cnt2++;
            }
            m = j + 1;
           // cout << cnt2 << endl;
            if(num[m] == m)
            {
                cout << j << endl;
                //printf("...\n");
            }
            else
            {
               int h = cnt2 - _num1 + 1;
            //cout << h << " " << endl;
                while(h--)
                {
                    y = m % 10;
                    m /= 10;
                }
                 cout << y << endl;
            }

        }

    }
    return 0;
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator