본문 바로가기
Develop/백준 (Cpp)

[백준] 1755번 : 숫자놀이 (C++)

by Tarra 2023. 2. 20.

1755번 : 숫자놀이


문제 )

79를 영어로 읽되 숫자 단위로 하나씩 읽는다면 "seven nine"이 된다. 80은 마찬가지로 "eight zero"라고 읽는다. 79는 80보다 작지만, 영어로 숫자 하나씩 읽는다면 "eight zero"가 "seven nine"보다 사전순으로 먼저 온다.

문제는 정수 M, N(1 ≤ M ≤ N ≤ 99)이 주어지면 M 이상 N 이하의 정수를 숫자 하나씩 읽었을 때를 기준으로 사전순으로 정렬하여 출력하는 것이다.

 

 

입력 :

첫째 줄에 M과 N이 주어진다.

 

 

 

출력 :

M 이상 N 이하의 정수를 문제 조건에 맞게 정렬하여 한 줄에 10개씩 출력한다.

 

 

 

 

 

 

풀이)

숫자를 문자로 입력 받은 뒤 해당 숫자에 따라 단어를 따로 생성해서 비교해주었다.

 

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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
 
using namespace std;
 
void make_word(string& temp, string& a)
{
    for (int i = 0; i < a.length(); i++)
    {
        if (a[i] == '0')
        {
            temp += "zero";
        }
        else if (a[i] == '1'
        {
            temp += "one";
        }
        else if (a[i] == '2')
        {
            temp += "two";
        }
        else if (a[i] == '3')
        {
            temp += "three";
        }
        else if (a[i] == '4')
        {
            temp += "four";
        }
        else if (a[i] == '5')
        {
            temp += "five";
        }
        else if (a[i] == '6')
        {
            temp += "six";
        }
        else if (a[i] == '7')
        {
            temp += "seven";
        }
        else if (a[i] == '8')
        {
            temp += "eight";
        }
        else if (a[i] == '9')
        {
            temp += "nine";
        }
 
        if (i == 0) temp += " ";
    }
}
 
bool compare(string a, string b)
{
    string temp_a = "", temp_b = "";
    
    make_word(temp_a, a);
    make_word(temp_b, b);
 
    return temp_a < temp_b;
}
 
int main()
{
    int m, n;
    cin >> m >> n;
 
    vector<string> vec;
    for (int i = m; i <= n; i++)
    {
        vec.push_back(to_string(i));
    }
 
    sort(vec.begin(), vec.end(), compare);
 
    int len = vec.size();
    int count = 0;
    for (int i = 0; i < len; i++)
    {
        if (count == 10)
        {
            cout << "\n";
            count = 0;
        }
        cout << vec[i] << " ";
        count++;
    }
 
    return 0;
}
cs

출처 : https://www.acmicpc.net/problem/1755 

 

1755번: 숫자놀이

79를 영어로 읽되 숫자 단위로 하나씩 읽는다면 "seven nine"이 된다. 80은 마찬가지로 "eight zero"라고 읽는다. 79는 80보다 작지만, 영어로 숫자 하나씩 읽는다면 "eight zero"가 "seven nine"보다 사전순으로

www.acmicpc.net