[백준 4287] Word Ratios (C#, C++) - soo:bak
작성일 :
문제 링크
설명
세 단어가 주어질 때 첫 번째에서 두 번째로의 문자 이동을 세 번째에 적용해 네 번째 단어를 만드는 문제입니다.
접근법
각 위치에서 첫 단어→둘째 단어로의 이동량을 계산하고, 같은 이동을 셋째 단어에 적용합니다.
알파벳은 a~z가 순환하므로 범위를 넘으면 다시 돌아오게 처리합니다.
Code
C#
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
using System;
using System.Text;
class Program {
static void Main() {
string? line;
var sb = new StringBuilder();
while ((line = Console.ReadLine()) != null) {
if (line == "#") break;
var parts = line.Split();
var a = parts[0];
var b = parts[1];
var c = parts[2];
var n = a.Length;
var res = new char[n];
for (var i = 0; i < n; i++) {
var diff = b[i] - a[i];
var v = c[i] - 'a' + diff;
v %= 26;
if (v < 0) v += 26;
res[i] = (char)('a' + v);
}
sb.Append(a).Append(' ').Append(b).Append(' ').Append(c).Append(' ').Append(res).AppendLine();
}
Console.Write(sb);
}
}
C++
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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string line;
while (getline(cin, line)) {
if (line == "#") break;
stringstream ss(line);
string a, b, c;
ss >> a >> b >> c;
int n = a.size();
string res(n, 'a');
for (int i = 0; i < n; i++) {
int diff = b[i] - a[i];
int v = c[i] - 'a' + diff;
v %= 26;
if (v < 0) v += 26;
res[i] = char('a' + v);
}
cout << a << " " << b << " " << c << " " << res << "\n";
}
return 0;
}