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

来源:互联网 发布:数据黑产吧 编辑:程序博客网 时间:2024/05/22 15:11
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
题意:1234567891011....这样的一串序列,问你第几数字是多少?
思路:分个位,十位,百位进行讨论,个位时直接输出,十位数时,n小于190,ans=n-10,然后讨论ans如果是偶数则是十位的数,ans除以20再加1就是结果。如果ans是奇数,与20取模,除以2就是结果。百位数时,也是同样进行讨论即可。

#include <stdio.h>#include <iostream>using namespace std;int main(){    int n;    while(~scanf("%d",&n))    {    if(n<=9)    {        printf("%d\n",n);        continue;    }    if(n<=189)    {        int a=n-10;        int b=a/20;        int c=a%20;        if(c%2==0)        {          printf("%d\n",b+1);          continue;        }        if(c%2==1)        {        printf("%d\n",c/2);        continue;        }    }      if(n<=1000)    {        int a=n-190;        int d=a/300;        a=a-(d)*300;        if(a%3==0)         {             printf("%d\n",d+1);             continue;         }         int b=a/30;         int c=a%30;         if(a%3==1)         {             printf("%d\n",b);             continue;         }         if(a%3==2)         {             printf("%d\n",c/3);             continue;         }    }    }}

总结:利用分类讨论的思想。
0 0
原创粉丝点击