codeforces_672A. Summer Camp(string)

来源:互联网 发布:centos gcc安装包下载 编辑:程序博客网 时间:2024/06/04 08:02
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.


水题,不过看到别人用了我没用到的string中的to_string()方法,还是积累一下,不过有的编译器可能编译不了。

我一开始的代码
#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <cmath>#include <stack>#include <bitset>#include <queue>#include <set>#include <map>#include <string>#include <algorithm>#define Si(a) scanf("%d",&a)#define Sl(a) scanf("%lld",&a)#define Sd(a) scanf("%lf",&a)#define Ss(a) scanf("%s",a)#define Pi(a) printf("%d\n",(a))#define Pl(a) printf("%lld\n",(a))#define Pd(a) printf("%lf\n",(a))#define Ps(a) printf("%s\n",(a))#define W(a) while(a--)#define mem(a,b) memset(a,(b),sizeof(a))#define FOP freopen("data.txt","r",stdin)#define inf 0x3f3f3f3f#define maxn 1000010#define mod 1000000007#define PI acos(-1.0)#define LL long longusing namespace std;char a[10010];int main(){    int i,n;    mem(a,'\0');    for(i=1;i<=500;i++)    {        char c[10];        sprintf(c,"%d",i);        strcat(a,c);    }    Si(n);    printf("%c\n",a[n-1]);    return 0;}

用上string的方法
#include <cstdio>#include <iostream>#include <algorithm>#include <iterator>#include <string>#include <vector>#include <set>#include <deque>#include <map>using namespace std;typedef long long LL;typedef pair<int, int> PII;#define MP make_pair#define FOR(v,p,k) for(int v=p;v<=k;++v)#define FORD(v,p,k) for(int v=p;v>=k;--v)#define REP(i,n) for(int i=0;i<(n);++i)#define VAR(v,i) __typeof(i) v=(i)#define FOREACH(i,c) for(VAR(i,(c).begin());i!=(c).end();++i)#define PB push_back#define ST first#define ND second#define SIZE(x) (int)x.size()#define ALL(c) c.begin(),c.end()int main(){string s;REP(i, 1000) {if (s.size() > 1000) {break;}int num = i + 1;s += to_string(num);}int n;cin >> n;cout << s[n - 1];    return 0;}





0 0
原创粉丝点击