※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 10193번 문제인 Word Swap이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/10193
10193번: Word Swap
Agnes has heard that the carnival is going to have a new game next year. The carnival guy chooses a word and writes it down on a piece of paper, and only tells you the length of the word (lets call it Word 1). You have to guess a word of the same length (l
www.acmicpc.net
각 소문자 'a-z'는 아스키코드에 97번부터 122번까지 차례대로 배정되어 있음을 상기하자. 따라서 문제에서 구하고자하는 값은 첫번째 문자열의
지문에 나와있듯이 정확한 출력 형식을 확인하기 위해 제출 창에서 Java 코드를 꼭 열어보도록 하자. 위에서 계산한 값이 음수일 경우 "earned"가 아닌 "cost"를 이용한 별도의 출력형식이 지정되어 있음을 알 수 있다.
아래는 제출한 소스코드이다.
#include <iostream>
using namespace std;
int T;
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> T;
while (T--) {
int ans = 0;
string s1, s2; cin >> s1 >> s2;
for (auto& l : s1) ans += l;
for (auto& l : s2) ans -= l;
if (ans > 0) cout << "Swapping letters to make " << s1 << " look like " << s2 << " earned " << ans << " coins.\n";
else if (ans < 0) cout << "Swapping letters to make " << s1 << " look like " << s2 << " cost " << -ans << " coins.\n";
else cout << "Swapping letters to make " << s1 << " look like " << s2 << " was FREE.\n";
}
}
'BOJ' 카테고리의 다른 글
[BOJ 14716 // C++] 현수막 (0) | 2023.12.13 |
---|---|
[BOJ 28353 // C++] 고양이 카페 (0) | 2023.12.12 |
[BOJ 13322 // C++] 접두사 배열 (0) | 2023.12.10 |
[BOJ 10195 // C++] Underwater Trip (0) | 2023.12.09 |
[BOJ 10194 // C++] Aligned Calender (0) | 2023.12.08 |