2024. 8. 23. 14:40ㆍIT/BaekJoon
문제 링크
https://www.acmicpc.net/problem/7602
문제
Our local gym has a number of exercise machines, each of which has a number of "levels". Some levels are harder than others, and exercising at those levels uses more energy per minute than at the easier levels. In this problem you have to work out how much energy a person would use up during an exercise session.
입력
Input will consists of a number of scenarios, each representing a machine and a number of people using the machine for exercise. Each scenario will begin with an integer, n, the number of levels available on the machine (0 < n < 10). The next n lines define the energy expended per minute at one level of the machine. The first line represents level 1, the second level 2 and so on.
There then follows data about a number of people who are exercising on the machine described. The first line of data gives the name of the person who is exercising (a single series of between 2 and 10 letters, lower case except for the first) followed by a space, followed by e, the number of exercises the person carried out (0 < e <= 50).
The next e lines contain the exercises carried out by the person. These consist of an integer giving the level, followed by a space, followed by d, the duration of the exercise in minutes.
The list of people is terminated by a line containing # 0. This line should not be processed.
Input is terminated by a scenario where n is 0 – this scenario should not be processed.
출력
Output is in sections, one section for each machine. Machines must be numbered, starting at 1. For each machine, one line is output for each person using the machine. This output is the name of the person, followed by a space, followed by the total amount of energy they expended carrying out their set of exercises. The people should be output in the order they appear in the input.
통과한 답안
namespace _7602
{
internal class Program
{
static void Main(string[] args)
{
int machineCnt = 1;
while (true)
{
int n = int.Parse(Console.ReadLine());
if (n == 0)
{
return;
}
int[] levels = new int[n];
for (int i = 0; i < n; i++)
{
levels[i] = int.Parse(Console.ReadLine());
}
Console.WriteLine($"Machine {machineCnt}");
machineCnt++;
while (true)
{
string[] inputs = Console.ReadLine().Split(' ');
if (inputs[0] == "#" && inputs[1] == "0")
{
break;
}
string name = inputs[0];
int times = int.Parse(inputs[1]);
int usedEnergy = 0;
for (int i = 0; i < times; i++)
{
string[] data = Console.ReadLine().Split(' ');
int level = int.Parse(data[0]);
int cnt = int.Parse(data[1]);
usedEnergy += levels[level - 1] * cnt;
}
Console.WriteLine($"{name} {usedEnergy}");
}
}
}
}
}
운동 기구에 대한 레벨에 따른 소모 에너지가 주어지고
이를 이용하는 사람의 이름과 횟수가 주어진다.
이 때, 각 횟수마다 사용하는 레벨과 시간이 다를 때,
각각의 사람들이 소모한 에너지를 구하는 문제이다.
입력값이 많아서 이를 적절하게 처리하는 것이 중요한 문제로
0이나 (# 0)이 출현하면 루프를 종료하게 while (true)문을 이용하여 구현하였다.
운동 기구의 소모 에너지를 배열로 저장한 후에
사람의 이름과 횟수를 받아서 이 횟수에 소모한 에너지들을 더한 후에 출력하도록 하였다.
'IT > BaekJoon' 카테고리의 다른 글
[BAEKJOON] 백준 25904: 안녕 클레오파트라 세상에서 제일가는 포테이토칩 (C#) (0) | 2024.08.24 |
---|---|
[BAEKJOON] 백준 5464: 주차장 (C#) (0) | 2024.08.23 |
[BAEKJOON] 백준 9289: Morse Code (C#) (0) | 2024.08.22 |
[BAEKJOON] 백준 5567: 결혼식 (C#) (0) | 2024.08.22 |
[BAEKJOON] 백준 17253: 삼삼한 수 2 (C#) (0) | 2024.08.22 |