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 uuuouou at 2014-09-29 19:43:17 on Problem 1019
#include <cstdio>
#include <algorithm>
using namespace std;
#define MAX     35000

unsigned group[MAX+1] = {0}, sumGroup[MAX+1] = {0};
inline unsigned getGroupLength(unsigned k)
{
    if(k <= 9)    return k;
    if(k <= 99)   return k + (k-9);
    if(k <= 999)  return k + (99-9) + 2*(k-99);
    if(k <= 9999) return k + (99-9) + 2*(999-99) + 3*(k-999);
                  return k + (99-9) + 2*(999-99) + 3*(9999-999) + 4*(k-9999);
}
void init()
{
    for(unsigned i = 1; i <= MAX; ++i){
        group[i] = getGroupLength(i);
        sumGroup[i] = sumGroup[i-1] + group[i];
    }
}
char getDigit(unsigned n)
{
    char num[12];
//get which group the nth digit is in
    unsigned k = lower_bound(sumGroup, sumGroup+MAX+1, n) - sumGroup;
//    printf("the %uth digit is in the %uth group\n", n, k);
//get which position the nth digit is in the kth group
    n -= sumGroup[k-1];
//get which number the nth digit is in the kth group
    k = lower_bound(group, group+MAX+1, n) - group;
//get the number in string
    sprintf(num, "%u", k);
//get the digit
    return num[n-group[k-1]-1];
}

int main()
{
    init();
    unsigned t, n;
    for(scanf("%u", &t); t--; ){
        scanf("%u", &n);
        putchar(getDigit(n));
        putchar('\n');
    }
    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