작성일 :

문제 링크

1440번 - 타임머신

설명

세 개의 두 자리 숫자가 주어질 때, 시·분·초로 배치할 수 있는 경우의 수를 구하는 문제입니다.


접근법

세 숫자를 시, 분, 초에 배치하는 6가지 순열을 모두 확인합니다.

시는 1에서 12 사이, 분과 초는 0에서 59 사이일 때만 유효한 경우로 카운트합니다.


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
using System;

class Program {
  static bool IsHour(int x) => x >= 1 && x <= 12;
  static bool IsMS(int x) => x >= 0 && x <= 59;

  static void Main() {
    var s = Console.ReadLine()!;
    var a = int.Parse(s.Substring(0, 2));
    var b = int.Parse(s.Substring(3, 2));
    var c = int.Parse(s.Substring(6, 2));
    var v = new[] { a, b, c };

    var cnt = 0;
    int[,] perm = { {0,1,2}, {0,2,1}, {1,0,2}, {1,2,0}, {2,0,1}, {2,1,0} };
    for (var i = 0; i < 6; i++) {
      var h = v[perm[i,0]];
      var m = v[perm[i,1]];
      var sec = v[perm[i,2]];
      if (IsHour(h) && IsMS(m) && IsMS(sec)) 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
22
23
24
25
26
27
28
29
30
31
32
33
#include <bits/stdc++.h>
using namespace std;

bool isHour(int x) { return x >= 1 && x <= 12; }
bool isMS(int x) { return x >= 0 && x <= 59; }

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

  string s; cin >> s;
  int a = stoi(s.substr(0, 2));
  int b = stoi(s.substr(3, 2));
  int c = stoi(s.substr(6, 2));
  int v[3] = {a, b, c};

  int perm[6][3] = {
    {0,1,2}, {0,2,1}, {1,0,2},
    {1,2,0}, {2,0,1}, {2,1,0}
  };

  int cnt = 0;
  for (int i = 0; i < 6; i++) {
    int h = v[perm[i][0]];
    int m = v[perm[i][1]];
    int sec = v[perm[i][2]];
    if (isHour(h) && isMS(m) && isMS(sec)) cnt++;
  }

  cout << cnt << "\n";

  return 0;
}