[백준 26336] Are We Stopping Again? (C#, C++) - soo:bak
작성일 :
문제 링크
26336번 - Are We Stopping Again?
설명
주행 거리 D와 주유/식사 주기가 주어질 때 정차 횟수를 계산하는 문제입니다.
접근법
목적지에서는 정차하지 않으므로 시작점부터 목적지 직전까지의 거리에서 정차 위치를 셉니다.
이후 주유 주기와 식사 주기의 배수 개수를 포함배제 원리로 계산하면 됩니다.
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
26
27
28
29
using System;
class Program {
static int Gcd(int a, int b) {
while (b != 0) {
var t = a % b;
a = b;
b = t;
}
return a;
}
static void Main() {
var t = int.Parse(Console.ReadLine()!);
for (var i = 0; i < t; i++) {
var parts = Console.ReadLine()!.Split();
var d = int.Parse(parts[0]);
var g = int.Parse(parts[1]);
var f = int.Parse(parts[2]);
var limit = d - 1;
var lcm = g / Gcd(g, f) * f;
var stops = limit / g + limit / f - limit / lcm;
Console.WriteLine($"{d} {g} {f}");
Console.WriteLine(stops);
}
}
}
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
#include <bits/stdc++.h>
using namespace std;
int gcdInt(int a, int b) {
while (b != 0) {
int t = a % b;
a = b;
b = t;
}
return a;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t; cin >> t;
while (t--) {
int d, g, f; cin >> d >> g >> f;
int limit = d - 1;
int lcm = g / gcdInt(g, f) * f;
int stops = limit / g + limit / f - limit / lcm;
cout << d << " " << g << " " << f << "\n";
cout << stops << "\n";
}
return 0;
}