작성일 :

문제 링크

13064번 - Cameras

설명

차량 번호가 여러 개 주어질 때, 규격에 맞는 번호만 입력 순서대로 출력하는 문제입니다.

번호는 8자리이며, 앞 두 자리는 같은 숫자, 그 다음 두 자리는 숫자, 다섯 번째는 대문자, 마지막 세 자리는 숫자여야 합니다.


접근법

각 번호에 대해 조건을 순서대로 확인합니다. 모든 조건을 만족하면 출력합니다.



Code

C#

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System;

class Program {
  static bool IsDigit19(char c) => c >= '1' && c <= '9';

  static bool Valid(string s) {
    if (s.Length != 8) return false;
    if (!(IsDigit19(s[0]) && s[0] == s[1])) return false;
    if (!(IsDigit19(s[2]) && IsDigit19(s[3]))) return false;
    if (!(s[4] >= 'A' && s[4] <= 'Z')) return false;
    if (!(IsDigit19(s[5]) && IsDigit19(s[6]) && IsDigit19(s[7]))) return false;
    return true;
  }

  static void Main() {
    var n = int.Parse(Console.ReadLine()!);
    for (var i = 0; i < n; i++) {
      var s = Console.ReadLine()!;
      if (Valid(s)) Console.WriteLine(s);
    }
  }
}

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;

bool isDigit19(char c) { return c >= '1' && c <= '9'; }

bool valid(const string& s) {
  if (s.size() != 8) return false;
  if (!(isDigit19(s[0]) && s[0] == s[1])) return false;
  if (!(isDigit19(s[2]) && isDigit19(s[3]))) return false;
  if (!(s[4] >= 'A' && s[4] <= 'Z')) return false;
  if (!(isDigit19(s[5]) && isDigit19(s[6]) && isDigit19(s[7]))) return false;
  return true;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);

  int n; cin >> n;
  string s;
  for (int i = 0; i < n; i++) {
    cin >> s;
    if (valid(s)) cout << s << "\n";
  }

  return 0;
}

Tags: 13064, BOJ, C#, C++, 구현, 문자열, 백준, 알고리즘

Categories: ,