작성일 :

문제 링크

25815번 - Cat’s Age

설명

고양이의 나이가 년/월 단위로 주어질 때, 1년 차는 15년, 2년 차는 9년, 이후 매년 4년에 해당하는 사람 나이로 환산하는 문제입니다. 1년 미만, 1~2년 사이, 2년 이상에 대해 월 단위 환산값이 다르게 주어지므로 이를 반영해 사람 나이(년, 월)를 출력합니다.


접근법

고양이 나이에 따라 환산 비율이 다르므로 구간별로 나누어 계산합니다. 1년 미만일 때는 고양이 1개월이 사람 15개월에 해당하고, 1년 차에는 고양이 1개월이 사람 9개월에 해당합니다. 2년 이상부터는 고양이 1년이 사람 4년, 1개월이 4개월에 해당합니다.

모든 계산을 월 단위로 통일한 뒤, 12로 나누어 년과 월을 구하면 됩니다.


Code

C#

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

class Program {
  static void Main() {
    var parts = Console.ReadLine()!.Split();
    var y = int.Parse(parts[0]);
    var m = int.Parse(parts[1]);

    var totalMonths = 0;
    if (y == 0) totalMonths = m * 15;
    else if (y == 1) totalMonths = 15 * 12 + m * 9;
    else totalMonths = (15 + 9 + (y - 2) * 4) * 12 + m * 4;

    var humanYears = totalMonths / 12;
    var humanMonths = totalMonths % 12;
    Console.WriteLine($"{humanYears} {humanMonths}");
  }
}

C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
using namespace std;

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

  int y, m; cin >> y >> m;

  int totalMonths = 0;
  if (y == 0) totalMonths = m * 15;
  else if (y == 1) totalMonths = 15 * 12 + m * 9;
  else totalMonths = (15 + 9 + (y - 2) * 4) * 12 + m * 4;

  int humanYears = totalMonths / 12;
  int humanMonths = totalMonths % 12;
  cout << humanYears << " " << humanMonths << "\n";

  return 0;
}