작성일 :

문제 링크

33191번 - Yalda

설명

예산 b와 수박, 석류, 견과류 가격이 주어질 때, 선호도 순서(수박 > 석류 > 견과류)대로 살 수 있는 첫 항목을 출력하고 아무것도 못 사면 Nothing을 출력하는 문제입니다.


접근법

선호 순서대로 가격을 비교해 처음으로 예산 이내인 항목을 출력하면 됩니다. 세 항목 모두 예산을 넘으면 Nothing을 출력합니다.



Code

C#

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

class Program {
  static void Main() {
    var b = int.Parse(Console.ReadLine()!);
    var w = int.Parse(Console.ReadLine()!);
    var p = int.Parse(Console.ReadLine()!);
    var n = int.Parse(Console.ReadLine()!);

    if (w <= b) Console.WriteLine("Watermelon");
    else if (p <= b) Console.WriteLine("Pomegranates");
    else if (n <= b) Console.WriteLine("Nuts");
    else Console.WriteLine("Nothing");
  }
}

C++

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

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

  int b, w, p, n;
  cin >> b >> w >> p >> n;

  if (w <= b) cout << "Watermelon\n";
  else if (p <= b) cout << "Pomegranates\n";
  else if (n <= b) cout << "Nuts\n";
  else cout << "Nothing\n";

  return 0;
}

Tags: 33191, BOJ, C#, C++, 구현, 백준, 알고리즘

Categories: ,