본문 바로가기
문제 해결/BaekJoon

[백준] [C++] 5218번 알파벳 거리

by WSLim_97 2023. 1. 19.
반응형

https://www.acmicpc.net/problem/5218

 

5218번: 알파벳 거리

첫째 줄에 테스트 케이스의 수 (< 100)가 주어진다. 각 테스트 케이스는 한 줄로 이루어져 있고, 두 단어가 공백으로 구분되어져 있다. 단어의 길이는 4보다 크거나 같고, 20보다 작거나 같으며, 알

www.acmicpc.net


코드

#include <iostream>
using namespace std;

int main()
{
	int n;
	string str1, str2;
	cin >> n;

	for (int i = 0; i < n; i++)
	{
		cin >> str1 >> str2;

		cout << "Distances: ";
		for (int j = 0; j < str1.length(); j++)
		{
			int distance;
			if (str2[j] >= str1[j])
				distance = str2[j] - str1[j];
			else
				distance = (str2[j] + 26) - str1[j];

			cout << distance << " ";
		}

		cout << endl;
	}

	return 0;
}

풀이

같은 길이의 단어 2개가 주어졌을 때 각 단어에 포함된 모든 글자의 알파벳 거리를 구하는 문제이다.

반응형