[백준 13698] Hawk eyes (C#, C++) - soo:bak
작성일 :
문제 링크
설명
컵 위치를 바꾸는 연산을 수행한 뒤 공의 최종 위치를 구하는 문제입니다.
접근법
컵 배열을 초기화하고 문자를 읽으며 해당하는 두 인덱스를 교환합니다.
마지막에 배열을 순회해 작은 공과 큰 공의 위치를 찾아 출력합니다.
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;
class Program {
static void Main() {
var s = Console.ReadLine()!;
var cup = new int[] {1, 0, 0, 2};
foreach (var ch in s) {
switch (ch) {
case 'A': Swap(ref cup[0], ref cup[1]); break;
case 'B': Swap(ref cup[0], ref cup[2]); break;
case 'C': Swap(ref cup[0], ref cup[3]); break;
case 'D': Swap(ref cup[1], ref cup[2]); break;
case 'E': Swap(ref cup[1], ref cup[3]); break;
case 'F': Swap(ref cup[2], ref cup[3]); break;
}
}
var small = 0; var big = 0;
for (var i = 0; i < 4; i++) {
if (cup[i] == 1) small = i + 1;
else if (cup[i] == 2) big = i + 1;
}
Console.WriteLine(small);
Console.WriteLine(big);
}
static void Swap(ref int a, ref int b) {
int t = a; a = b; b = t;
}
}
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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string s;
if (!(cin >> s)) return 0;
int cup[4] = {1, 0, 0, 2};
auto swp = [&](int i, int j) { swap(cup[i], cup[j]); };
for (char ch : s) {
if (ch == 'A') swp(0, 1);
else if (ch == 'B') swp(0, 2);
else if (ch == 'C') swp(0, 3);
else if (ch == 'D') swp(1, 2);
else if (ch == 'E') swp(1, 3);
else if (ch == 'F') swp(2, 3);
}
int small = 0, big = 0;
for (int i = 0; i < 4; i++) {
if (cup[i] == 1) small = i + 1;
else if (cup[i] == 2) big = i + 1;
}
cout << small << "\n" << big << "\n";
return 0;
}