๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿ“š ์ „๊ณต ๊ณต๋ถ€/์•Œ๊ณ ๋ฆฌ์ฆ˜ ํ•ด์„ ๋ฐ ์„ค๊ณ„

[์•Œ๊ณ ๋ฆฌ์ฆ˜] ๋ฌธ์ž์—ด ๋‹ค๋ฃจ๊ธฐ - ๋กœ๋งˆ์ˆซ์ž(cpp)

728x90

๋กœ๋งˆ์ธ๋“ค์€ ๋กœ๋งˆ ์ˆซ์ž๋ฅผ ์‚ฌ์šฉํ–ˆ๋‹ค. 1์€ I, 5๋Š” V, 10์€ X๋กœ ํ‘œํ˜„ํ–ˆ๊ณ , I, V, X๊ฐ€ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด์€ ๊ฐ ๊ธ€์ž๊ฐ€ ๋‚˜ํƒ€๋‚ด๋Š” ์ˆ˜์˜ ํ•ฉ์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด XVI = 10 + 5 + 1 = 16์ด๋‹ค.
๋‹จ, IV๋Š” 4์ด๊ณ  IX๋Š” 9์ด๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด IVI = 4 + 1 = 5์ด๋‹ค. 
์œ„์˜ ์˜ˆ์™ธ์— ์ถ”๊ฐ€๋กœ, IIV๋Š” 3์ด๊ณ  IIX๋Š” 8์ด๋ผ๊ณ  ํ•˜์ž. ๋ฐ˜๋“œ์‹œ IIV, IIX๋Š” 3, 8์ด์–ด์•ผ ํ•œ๋‹ค.
์ฆ‰ IIV๋Š” 1 + 1 + 5 = 7๋„ ์•„๋‹ˆ๊ณ , 1 + 4 = 5๋„ ์•„๋‹ˆ๊ณ , ๋ฐ˜๋“œ์‹œ 3์ด์–ด์•ผ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

์ตœ๋Œ€ ๊ธธ์ด๊ฐ€ 10์ธ ๋กœ๋งˆ ์ˆซ์ž๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์งˆ ๋•Œ ์ด ๋ฌธ์ž์—ด์ด ๋‚˜ํƒ€๋‚ด๋Š” ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ
ํ‘œ์ค€ ์ž…๋ ฅ์œผ๋กœ ์ž…๋ ฅ์„ ๋ฐ›๋Š”๋‹ค. ์ตœ๋Œ€ ๊ธธ์ด๊ฐ€ 10์ธ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง€๊ณ , ์ด ๋ฌธ์ž์—ด์€ I, V, X๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

์ถœ๋ ฅ 
ํ‘œ์ค€ ์ถœ๋ ฅ์œผ๋กœ ์ˆซ์ž ํ•˜๋‚˜๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค. ์ด ์ˆ˜๋Š” ์ž…๋ ฅ๋œ ๋ฌธ์ž์—ด์ด ํ‘œํ˜„ํ•˜๋Š” ์ˆ˜์ด๋‹ค.

์˜ˆ์ œ ์ž…๋ ฅ 1
XIIV
์˜ˆ์ œ ์ถœ๋ ฅ 1
13

 

๋ฌธ์ž์—ด ๋‹ค๋ฃจ๊ธฐ ๊ณผ์ œ์ด๋‹ค!

IV๋Š” 4์ด๊ณ  IX๋Š” 9์ด๋‹ค. & ๋ฐ˜๋“œ์‹œ IIV, IIX๋Š” 3, 8์ด์–ด์•ผ ํ•œ๋‹ค.

๋ผ๋Š” ์กฐ๊ฑด์ด ์žˆ๊ธฐ ๋•Œ๋ฌธ์—, ์ด๋ฅผ ์šฐ์„ ์ˆœ์œ„๋กœ ๋จผ์ € ์ฒ˜๋ฆฌํ•ด์ฃผ์—ˆ๋‹ค.

 

#include <iostream>
#include <vector>
#include <string>
using namespace std;

// I, II, IIV, IV, V, VI, VII, IIX, IX, X

int main(){
    ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);

    vector<string> RomaNums = {"IIV", "IV", "IIX", "IX"}; // 3, 4, 8, 9
    string numbers;
    int cnt = 0;

    cin >> numbers;

    for (int i = 0; i < 4; ++i) {
        int index = numbers.find(RomaNums[i]);
        if (index != string::npos) {
            switch(i){
                case 0:
                    numbers.erase(numbers.begin() + index, numbers.begin() + (index + 3));
                    cnt += 3;
                    break;
                case 1:
                    numbers.erase(numbers.begin() + index, numbers.begin() + (index + 2));
                    cnt += 4;
                    break;
                case 2:
                    numbers.erase(numbers.begin() + index, numbers.begin() + (index + 3));
                    cnt += 8;
                    break;
                case 3:
                    numbers.erase(numbers.begin() + index, numbers.begin() + (index + 2));
                    cnt += 9;
                    break;
            }
            i--;
        }
    }
    // cout << numbers << "\n";
    // cout << "count : " << cnt << "\n";
    int len = numbers.length();
    for (int i = 0; i < len; ++i)
    {
        if(numbers[i] == 'X') {
            numbers.erase(numbers.begin() + i);
            cnt += 10;
            i--;
        }
        if(numbers[i] == 'V') {
            numbers.erase(numbers.begin() + i);
            cnt += 5;
            i--;
        }
        if(numbers[i] == 'I') {
            numbers.erase(numbers.begin() + i);
            cnt += 1;
            i--;
        }
    }
    cout << cnt;
    cnt = 0;
    
    return 0;
}

 

728x90