[백준 31822] 재수강 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
기준 과목 코드와 앞 5자리가 같은 과목의 개수를 구하는 문제입니다.
접근법
기준 과목 코드의 앞 5글자를 저장해 둡니다.
이후 목록을 순회하며 각 과목 코드의 앞 5글자가 같은지 비교해 개수를 셉니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using System;
class Program {
static void Main() {
var baseCode = Console.ReadLine()!;
var n = int.Parse(Console.ReadLine()!);
var key = baseCode.Substring(0, 5);
var cnt = 0;
for (var i = 0; i < n; i++) {
var code = Console.ReadLine()!;
if (code.StartsWith(key)) cnt++;
}
Console.WriteLine(cnt);
}
}
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string baseCode; cin >> baseCode;
int n; cin >> n;
string key = baseCode.substr(0, 5);
int cnt = 0;
for (int i = 0; i < n; i++) {
string code; cin >> code;
if (code.compare(0, 5, key) == 0) cnt++;
}
cout << cnt << "\n";
return 0;
}