본문 바로가기
Develop/백준 (Cpp)

[백준] 2852번 : NBA 농구 (C++)

by Tarra 2023. 5. 10.

2852번 : NBA 농구


문제 )

동혁이는 NBA 농구 경기를 즐겨 본다. 동혁이는 골이 들어갈 때 마다 골이 들어간 시간과 팀을 적는 이상한 취미를 가지고 있다.

농구 경기는 정확히 48분동안 진행된다. 각 팀이 몇 분동안 이기고 있었는지 출력하는 프로그램을 작성하시오.

 

입력 :

첫째 줄에 골이 들어간 횟수 N(1<=N<=100)이 주어진다. 둘째 줄부터 N개의 줄에 득점 정보가 주어진다. 득점 정보는 득점한 팀의 번호와 득점한 시간으로 이루어져 있다. 팀 번호는 1 또는 2이다. 득점한 시간은 MM:SS(분:초) 형식이며, 분과 초가 한자리 일 경우 첫째자리가 0이다. 분은 0보다 크거나 같고, 47보다 작거나 같으며, 초는 0보다 크거나 같고, 59보다 작거나 같다. 득점 시간이 겹치는 경우는 없다.

 

 

 

출력 :

첫째 줄에 1번 팀이 이기고 있던 시간, 둘째 줄에 2번 팀이 이기고 있던 시간을 출력한다. 시간은 입력과 같은 형식(MM:SS)으로 출력한다.

 

 

 

 

풀이)

 

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <string>
 
using namespace std;
 
void print_time(int time)
{
 
    if (time / 60 < 10cout << "0";
    cout << time / 60 << ":";
    time %= 60;
 
    if (time < 10cout << "0";
    cout << time << "\n";
}
 
int main()
{
 
    int n;
    cin >> n;
    
    int winning_team = 0;
 
    string current_time;
    string prev_time;
 
    int winning_time_team1 = 0, winning_time_team2 = 0;
    int score_team1 = 0, score_team2 = 0;
 
    int team;
    for (int i = 0; i < n; i++)
    {
        cin >> team;
        cin >> current_time;
 
        // 두 팀의 점수가 동일할 경우
        // 시간 카운팅 x
        if (score_team1 == score_team2)
        {
            prev_time = current_time;
            winning_team = team;
 
            if (team == 1) score_team1++;
            else if (team == 2) score_team2++;
            continue;
        }
 
 
        // string을 int로 변환하여 잠시 가지고 있는다
        int temp_current_tiem = stoi(current_time.substr(02)) * 60 + stoi(current_time.substr(35));
        int temp_prev_time = stoi(prev_time.substr(02)) * 60 + stoi(prev_time.substr(35));
        
        // 어느팀이 이기고 있던 시간
        int temp_time = temp_current_tiem - temp_prev_time;
 
        if (score_team1 > score_team2)
        {
            winning_time_team1 += temp_time;
        }
        else if (score_team1 < score_team2)
        {
            winning_time_team2 += temp_time;
        }
 
        // 득점을 올려준다.
        if (team == 1) score_team1++;
        else if (team == 2) score_team2++;
        prev_time = current_time;
    }
 
    // 경기가 끝난 후.
    // 종료 직전 득점과 종료시간에 맞추어 한번 더 더해준다.
    int temp_time = 48 * 60 - (stoi(prev_time.substr(02)) * 60 + stoi(prev_time.substr(35)));
    if (score_team1 > score_team2)
    {
        winning_time_team1 += temp_time;
    }
    else if (score_team1 < score_team2)
    {
        winning_time_team2 += temp_time;
    }
 
    print_time(winning_time_team1);
    print_time(winning_time_team2);
 
    return 0;
}
 
cs

 


출처 : https://www.acmicpc.net/problem/2852 

 

2852번: NBA 농구

첫째 줄에 골이 들어간 횟수 N(1<=N<=100)이 주어진다. 둘째 줄부터 N개의 줄에 득점 정보가 주어진다. 득점 정보는 득점한 팀의 번호와 득점한 시간으로 이루어져 있다. 팀 번호는 1 또는 2이다. 득

www.acmicpc.net