Codeforces Round #352 (Div. 2) A. Summer Camp __ water problem

A. Summer Camp
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Every year, hundreds of people come to summer camps, they learn new algorithms and solve hard problems.

This is your first year at summer camp, and you are asked to solve the following problem. All integers starting with 1 are written in one line. The prefix of these line is "123456789101112131415...". Your task is to print the n-th digit of this string (digits are numbered starting with 1.

Input

The only line of the input contains a single integer n (1 ≤ n ≤ 1000) — the position of the digit you need to print.

Output

Print the n-th digit of the line.

Examples
input
3
output
3
input
11
output
0
Note

In the first sample the digit at position 3 is '3', as both integers 1 and 2 consist on one digit.

In the second sample, the digit at position 11 is '0', it belongs to the integer 10.


Source

.Codeforces Round #352 (Div. 2)  A. Summer Camp 



My Solutions

Use stringstream to build the line , then cout the str[n-1]

#include <iostream>
#include <cstdio>
#include <string>
#include <sstream>
using namespace std;

int main()
{
    int n;
    string line = "12345678910111213141516171819", str;
    scanf("%d", &n);

    for(int i = 20; i <= 500; i++){
        stringstream ss;
        ss<<i;
        ss>>str;
        line += str;
        //str.clear();
    }
    cout<<line[n-1];
    /*please ignore the follow code
    if(n <= 9) printf("%d", n);
    else if(n > 9 && n <= 29){
        if(n%2 == 0) printf("%d", 1);
        else{
            n-=9;
            printf("%d", n/2 - 1);
        }
    }
    else if(n > 29){
        n-=29;
        if(n%3 == 1) printf("%d", 1);
        else if(n%3 == 2) printf("%d", (n - 2 + 3)/3 - 1)
    }
*/
    return 0;
}

Thank you!

                                                                                                                                               ------from ProLights

你可能感兴趣的:(ACM,round,problem,codeforces,water,#35)