[백준 17487] 타자 연습 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
문장을 입력할 때 왼손과 오른손이 누르는 키 횟수를 계산하는 문제입니다.
접근법
먼저, 오른손으로 누르는 키 목록을 정의하고, 나머지는 왼손으로 처리합니다.
다음으로, 각 문자를 순회하며 공백과 대문자의 Shift 입력은 별도로 카운트합니다. 일반 키 입력은 해당 손에 바로 더합니다.
이후, 모아둔 공백과 Shift 입력을 현재 적게 사용한 손에 하나씩 분배하여 균형을 맞춥니다.
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
30
31
32
33
34
35
36
using System;
using System.Linq;
class Program {
static void Main() {
var rightKeys = new[] { 'u', 'i', 'o', 'p', 'h', 'j', 'k', 'l', 'n', 'm' };
var input = Console.ReadLine()!;
var left = 0;
var right = 0;
var shiftOrSpace = 0;
foreach (var ch in input) {
if (ch == ' ') {
shiftOrSpace++;
continue;
}
if (ch >= 'A' && ch <= 'Z') {
shiftOrSpace++;
var lower = (char)(ch + ('a' - 'A'));
if (rightKeys.Contains(lower)) right++;
else left++;
} else {
if (rightKeys.Contains(ch)) right++;
else left++;
}
}
while (shiftOrSpace-- > 0) {
if (left > right) right++;
else left++;
}
Console.WriteLine($"{left} {right}");
}
}
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
30
31
32
33
34
35
36
37
38
39
#include <bits/stdc++.h>
using namespace std;
typedef vector<char> vc;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
vc rightKeys = { 'u', 'i', 'o', 'p', 'h', 'j', 'k', 'l', 'n', 'm' };
string input; getline(cin, input);
int left = 0, right = 0, shiftOrSpace = 0;
for (char ch : input) {
if (ch == ' ') {
shiftOrSpace++;
continue;
}
if (ch >= 'A' && ch <= 'Z') {
shiftOrSpace++;
char lower = ch + ('a' - 'A');
if (find(rightKeys.begin(), rightKeys.end(), lower) != rightKeys.end()) right++;
else left++;
} else {
if (find(rightKeys.begin(), rightKeys.end(), ch) != rightKeys.end()) right++;
else left++;
}
}
while (shiftOrSpace--) {
if (left > right) right++;
else left++;
}
cout << left << ' ' << right << '\n';
return 0;
}