[백준 6811] Old Fishin’ Hole (C#, C++) - soo:bak
작성일 :
문제 링크
설명
세 종류의 물고기마다 점수가 주어지고 최대 허용 점수가 있습니다. 최소 한 마리 이상 잡으면서 허용 점수 이하인 모든 조합을 출력하는 문제입니다.
접근법
각 물고기 수를 0부터 가능한 최대치까지 삼중 반복으로 탐색합니다.
총점이 허용 범위 내이고 최소 한 마리 이상이면 출력하고 개수를 셉니다.
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 void Main() {
var t = int.Parse(Console.ReadLine()!);
var p = int.Parse(Console.ReadLine()!);
var w = int.Parse(Console.ReadLine()!);
var lim = int.Parse(Console.ReadLine()!);
var ways = 0;
for (var a = 0; a * t <= lim; a++) {
for (var b = 0; a * t + b * p <= lim; b++) {
for (var c = 0; a * t + b * p + c * w <= lim; c++) {
if (a == 0 && b == 0 && c == 0) continue;
var total = a * t + b * p + c * w;
if (total <= lim) {
Console.WriteLine($"{a} Brown Trout, {b} Northern Pike, {c} Yellow Pickerel");
ways++;
} else break;
}
}
}
Console.WriteLine($"Number of ways to catch fish: {ways}");
}
}
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
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t, p, w, lim;
cin >> t >> p >> w >> lim;
int ways = 0;
for (int a = 0; a * t <= lim; a++) {
for (int b = 0; a * t + b * p <= lim; b++) {
for (int c = 0; a * t + b * p + c * w <= lim; c++) {
if (a == 0 && b == 0 && c == 0) continue;
cout << a << " Brown Trout, " << b << " Northern Pike, " << c << " Yellow Pickerel\n";
ways++;
}
}
}
cout << "Number of ways to catch fish: " << ways << "\n";
return 0;
}