:: ADVANCE ::

[dovelet][다중 반복문] n번째 숫자 구하기 (se) 본문

Algorithm/구현 | ETC

[dovelet][다중 반복문] n번째 숫자 구하기 (se)

KSJ14 2014. 9. 23. 21:47
반응형

dovelet    4 단계  다중 반복문


n번째 숫자 구하기 (se)


http://59.23.113.171/30stair/se/se.php?pname=se


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <iostream>
using namespace std;
 
char temp[1000000];
int main(void)
{
    int n, i;
 
    cin >> n;
 
    i = 1;
    int index = 1;
    while(index <= n)    {
        if(i < 10)    {
            temp[index++] = i;
            i++;
        }
        else if(i >= 10 && i <100)    {
            temp[index++] = i / 10;
            temp[index++] = i % 10;
            i++;
        }
        else if(i >= 100 && i < 1000)    {
            temp[index++] = i / 100;
            temp[index++] = (i%100)/10;
            temp[index++] = i % 10;
            i++;
        }
        else if(i >= 1000 && i < 10000)    {
            temp[index++] = i / 1000;
            temp[index++] = (i%1000)/100;
            temp[index++] = (i%100)/10;
            temp[index++] = i % 10;
            i++;
        }
        else if(i >= 10000 && i < 100000)    {
            temp[index++] = i / 10000;
            temp[index++] = (i%10000)/1000;
            temp[index++] = (i%1000)/100;
            temp[index++] = (i%100)/10;
            temp[index++] = i % 10;
            i++;
        }
        else if(i >= 100000 && i < 1000000)    {
            temp[index++] = i / 100000;
            temp[index++] = (i%100000)/10000;
            temp[index++] = (i%10000)/1000;
            temp[index++] = (i%1000)/100;
            temp[index++] = (i%100)/10;
            temp[index++] = i % 10;
            i++;
        }
    }
 
    printf("%d\n", temp[n]);
 
    return 0;
}

 


2 차 풀이  문자열 함수 이용

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string.h>
using namespace std;
 
char number[1000010];
 
int main(void)
{
    int i, n, len = 0;
    cin >> n;
 
    for(i = 1; len < n; i++)    {
        sprintf(number, "%d", i);
        len += strlen(number);
    }
 
    printf("%c\n", number[strlen(number)-(len-n)-1]);
 
    return 0;
}


반응형
Comments