Little Jay

[C++] 백준 15312번 - 이름 궁합 본문

알고리즘/BOJ

[C++] 백준 15312번 - 이름 궁합

Jay, Lee 2022. 2. 6. 21:04

ascii 코드를 이용해서 잘 변환시키는게 중요했던 문제

처음에 알파벳 변환은 'A'의 아스키 값인 65를 빼주면 되고,

숫자를 임시적으로 문자열로 저장했을 때,

이를 다시 숫자로 변환할때는 0의 아스키 값인 48을 빼주면 된다.

#include <bits/stdc++.h>
#define endl '\n'
using namespace std;

int alphabet[] = { 3, 2, 1, 2, 3, 3, 2, 3, 3, 2, 2, 1, 2, 2, 1, 2, 2, 2, 1, 2, 1, 1, 1, 2, 2, 1 };
string s1, s2;

int main() {

	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);

	cin >> s1;
	cin >> s2;
	string temp_str;

	// char - 65 하면 index번호 나온다
	for (int i = 0; i < s1.size(); i++) {
		int temp = alphabet[(s1[i] - 65)];
		temp_str += to_string(temp);
		temp = alphabet[(s2[i] - 65)];
		temp_str += to_string(temp);
	}

	while(temp_str.size() > 2) {
		string s;
        // char to int => char - 48
		for (int i = 0; i < temp_str.size() - 1; i++) {
			int temp = ((temp_str[i] - 48) + (temp_str[i+1] - 48)) % 10;
			s += to_string(temp);
		}
		temp_str = s;
	}

	cout << temp_str << endl;

	return 0;
}
Comments