[백준 4872] SPIN (C#, C++) - soo:bak
작성일 :
문제 링크
설명
처음 바퀴 상태와 버튼 문자열들이 주어질 때, 버튼을 누른 순서대로 반영한 뒤 마지막 바퀴 상태를 구하는 문제입니다.
접근법
현재 바퀴 상태를 문자열이나 숫자 배열로 들고 있으면 됩니다.
첫 줄의 시작 상태를 저장한 뒤, 이후 줄에 나오는 버튼 문자열을 끝까지 읽습니다. 버튼 문자열의 각 자리 숫자를 현재 바퀴 값에 더하고 10으로 나눈 나머지로 바꾸면, 그 버튼을 한 번 누른 뒤의 상태가 됩니다.
이 과정을 모든 버튼에 대해 반복한 뒤 마지막 상태를 그대로 출력하면 됩니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using System;
class Program {
static void Main() {
string wheels = Console.ReadLine()!;
char[] state = wheels.ToCharArray();
string? button;
while ((button = Console.ReadLine()) != null) {
for (int i = 0; i < state.Length; i++) {
int value = (state[i] - '0') + (button[i] - '0');
state[i] = (char)('0' + (value % 10));
}
}
Console.WriteLine(new string(state));
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string state;
cin >> state;
string button;
while (cin >> button) {
for (int i = 0; i < (int)state.size(); i++) {
int value = (state[i] - '0') + (button[i] - '0');
state[i] = (char)('0' + (value % 10));
}
}
cout << state << "\n";
return 0;
}