[백준 29722] 브실혜성 (C#, C++) - soo:bak
작성일 :
문제 링크
설명
오늘 날짜와 주기가 주어질 때, 1년을 360일로 가정하여 다음에 브실혜성을 볼 수 있는 날짜를 구하는 문제입니다.
접근법
1년이 360일이고 매달 30일이므로 날짜를 총 일수로 변환하기 쉽습니다. 연도에 360을 곱하고, 월에서 1을 뺀 값에 30을 곱하고, 일에서 1을 뺀 값을 더하면 총 일수가 됩니다.
여기에 주기 N을 더한 뒤, 360으로 나누어 연도를, 나머지를 30으로 나누어 월과 일을 복원합니다. 월과 일은 1부터 시작하므로 각각 1을 더해줍니다.
Code
C#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 d = int.Parse(parts[2]);
var n = int.Parse(Console.ReadLine()!);
var total = y * 360 + (m - 1) * 30 + (d - 1) + n;
y = total / 360;
total %= 360;
m = total / 30 + 1;
d = total % 30 + 1;
Console.WriteLine($"{y:D4}-{m:D2}-{d:D2}");
}
}
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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
string date; cin >> date;
int y = stoi(date.substr(0, 4));
int m = stoi(date.substr(5, 2));
int d = stoi(date.substr(8, 2));
int n; cin >> n;
int total = y * 360 + (m - 1) * 30 + (d - 1) + n;
y = total / 360;
total %= 360;
m = total / 30 + 1;
d = total % 30 + 1;
cout << setfill('0') << setw(4) << y << "-"
<< setw(2) << m << "-" << setw(2) << d << "\n";
return 0;
}