codeforces 672A A. Summer Camp(水题)

题目链接:

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.

题意:

问自然数组成的字符串的第n位的数是多少;

思路:

n<=1000,所以直接暴力跑一波;

AC代码:

#include <bits/stdc++.h>
/*#include <iostream>
#include <queue>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=2e4+5;

int n;
int main()
{

    scanf("%d",&n);
    int cnt=0;
    for(int i=1;i<=1000;i++)
    {
        int temp=i,ans=0,num=0;
        while(temp)
        {
            ans=ans*10+temp%10;
            temp/=10;
            num++;
        }
        int s;
        while(num--)
        {
            s=ans%10;
            ans/=10;
            cnt++;
            if(cnt>=n)
            {
                cout<<s<<"
";
                return 0;
            }

        }
    }

    return 0;
}
原文地址:https://www.cnblogs.com/zhangchengc919/p/5484494.html