24h購物| | PChome| 登入
2012-05-19 13:14:39| 人氣653| 回應0 | 上一篇 | 下一篇

[UVA][Math] 10427 - Naughty Sleepy Boys

推薦 0 收藏 0 轉貼0 訂閱站台

Problem I
Naughty Sleepy Boys

Input: standard input
Output: standard output
Time Limit: 2 seconds

Hasan and Tanveer are two naughty boys of the class. They spent most of their class time playing `Tic Tac Toe' whenever they get a chance to sit in back bench. But their teacher doesn't think that playing cross and naught is a way of making a class enjoyable. So, whenever she sees them playing she brings them to the front seat and makes them hear what she says. And you know listening to the teacher is not as enjoyable as playing `Tic Tac Toe'. So, within a couple of minutes, their eyes become dizzy and they fall into sleep resting their heads on the table. The teacher notices that and exclaims, ``You two naughty boys! Come over here.'' and she gives them some problem to solve which is lengthy and cumbersome. They try all the time not to get caught by the teacher while playing, or, for the least, not to get sleepy. But, pity for them, they hardly escapes the eagle eye of the teacher. Now, they are again caught red handed, brought to front bench, and as you know the usual case, they become sleepy. (poor Hasan and Tanveer! Couldn't the teacher be a little bit sympathetic to them?) This time the teacher is very angry and gives them a problem which is really too much for them. She asks them if she writes down the numbers from 1 to 1000 one after another what will be the 1000-th digit. You see what a tough problem for two little boys. Alam and Dalim start thinking that if they write down all the numbers like 1234567891011121314 ... ... it will take many hours for them. Moreover, the answer will possibly be incorrect because they can easily lose track of the numbers written. So, they are trying to find out a way to fool the teacher with the right answer. Can you find a way out for them?

 

Input

The input will contain one or more line each containing a positive integer N (N<100000000). Total line of the input file will not exceed 11000.

 

Output

For every N, you should calculate the N-th digit of the number 123456789101112 ... ... and print it on a line itself.

 

Sample Input
3
9
10
11
10000
50000

 

Sample Output
3
9
1
0
7

1 


先找到它是在第n位數的, 接著統計n-1位數的總長度, 找出差n-sum-1, 最後找到還需要幾個數字diff,
接著最後的差就是在接下一個的數字的第幾個

#include <stdio.h>

int main() {
    int n;
    while(scanf("%d", &n) == 1) {
        int len, tmp = 9, sum = 0;
        for(len = 1; ; len++) {
            sum += tmp*len;
            if(sum >= n)
                break;
            tmp = tmp*10;
        }
        sum -= tmp*len;
        len--, tmp = tmp/9;
        int diff = (n-sum-1)/(len+1);
        int num = tmp+diff;
        n -= sum+diff*(len+1);
        char str[50];
        sprintf(str, "%d", num);
        printf("%c\n", str[n-1]);
    }
    return 0;
}

台長: Morris
人氣(653) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][模擬] 11926 - Multitasking
此分類上一篇:[UVA][Stack] 120 - Stacks of Flapjacks

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文