UVA10427 Naughty Sleepy Boys数学

Posted 海岛Blog

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA10427 Naughty Sleepy Boys数学相关的知识,希望对你有一定的参考价值。

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

问题链接UVA10427 Naughty Sleepy Boys
问题简述:从1开始往后写数字,构成一个如下的字符串 123456789101112… 。求第n位的数字是多少?
问题分析:简单题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA10427 Naughty Sleepy Boys */

#include <bits/stdc++.h>

using namespace std;

char str[12];

int main()
{
    int n;
    while (~scanf("%d", &n)) {
        int len, t = 9, sum = 0;
        for (len = 1; ;len++) {
            sum += t * len;
            if (sum >= n) break;
            t *= 10;
        }
        sum -= t * len;
        len--, t /= 9;
        int diff = (n - sum - 1) / (len + 1);
        int num = t + diff;
        n -= sum + diff * (len + 1);
        sprintf(str, "%d", num);

        printf("%c\\n", str[n - 1]);
    }

    return 0;
}

以上是关于UVA10427 Naughty Sleepy Boys数学的主要内容,如果未能解决你的问题,请参考以下文章

CF 936B Sleepy Game(判环+BFS)

Very Sleepy使用图文教程

sleepy lagoon

CodeForces - 937D Sleepy Game

B. Sleepy Game 博弈搜索

CodeForces 937D Sleepy Game