작성일 :

문제 링크

4636번 - Clay Bully

설명

학생마다 완성한 점토 블록의 가로, 세로, 높이와 이름이 주어집니다. 처음에 받은 점토 양은 모두 같으므로, 부피가 가장 큰 학생이 bully이고 부피가 가장 작은 학생이 victim입니다.


접근법

학생 한 명을 읽을 때마다 블록의 부피를 구합니다.

한 반의 모든 학생을 보면서 가장 큰 부피와 가장 작은 부피를 가진 학생 이름을 저장해 두면 됩니다. 입력이 끝난 뒤에는 문제에서 요구한 형식대로 bully took clay from victim.을 출력합니다.

입력은 여러 반이 이어지고, 학생 수가 -1이면 종료합니다.



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
30
31
32
33
34
35
36
37
38
using System;

class Program {
  static void Main() {
    while (true) {
      int n = int.Parse(Console.ReadLine()!);
      if (n == -1)
        break;

      string bully = "";
      string victim = "";
      int maxVolume = -1;
      int minVolume = int.MaxValue;

      for (int i = 0; i < n; i++) {
        string[] input = Console.ReadLine()!.Split();
        int x = int.Parse(input[0]);
        int y = int.Parse(input[1]);
        int z = int.Parse(input[2]);
        string name = input[3];

        int volume = x * y * z;

        if (volume > maxVolume) {
          maxVolume = volume;
          bully = name;
        }

        if (volume < minVolume) {
          minVolume = volume;
          victim = name;
        }
      }

      Console.WriteLine($"{bully} took clay from {victim}.");
    }
  }
}

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
30
31
32
33
34
35
36
37
38
39
40
41
#include <bits/stdc++.h>
using namespace std;

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

  while (true) {
    int n;
    cin >> n;

    if (n == -1)
      break;

    string bully, victim;
    int maxVolume = -1;
    int minVolume = INT_MAX;

    for (int i = 0; i < n; i++) {
      int x, y, z;
      string name;
      cin >> x >> y >> z >> name;

      int volume = x * y * z;

      if (volume > maxVolume) {
        maxVolume = volume;
        bully = name;
      }

      if (volume < minVolume) {
        minVolume = volume;
        victim = name;
      }
    }

    cout << bully << " took clay from " << victim << ".\n";
  }

  return 0;
}

Tags: 4636, BOJ, C#, C++, 구현, 백준, 시뮬레이션, 알고리즘

Categories: ,