2023년 12월 5일 화요일

C++(Algorithm) - 에어컨

 


머시기 알고리즘 경진대회에서 나왔다는 문제이다.


간단히 요약하면 승객이 탑승 했던 시간 데이터 배열 onboard 를 매개변수로 가져와 

승객이 탑승 중이면 실내 온도를 희망 온도 t1, t2 사이에서 유지시킨다. 

temperature은 실외 온도



 에어컨 작동 중일 때 희망, 실내 온도가 다르면 매분 a전력 소모하고 같다면 b를 소모함

에어컨을 키면 실내 온도는 희망 온도와 분당 1도씩 가까워짐

에어컨을 끄면 실내 온도는 실외 온도와 분당 1도씩 가까워짐

solution 함수의 리턴값은 승객이 탑승 중인 시간에 

실내온도를 유지하기 위한 최소 소비 전력이다. 

 



결과 예



제한 사항

입력 값만 제한하면 되므로 따로 코드로 안해줘도 될듯하다.


기본 코드 틀

1
2
3
4
5
6
7
8
9
10
#include <string>
#include <vector>
 
using namespace std;
 
int solution(int temperature, int t1, int t2, int a, int b, vector<int> onboard) 
{
    int answer = 0;
    return answer;
}

문제 풀 때마다 생각하는데 기본 코드가 별로인거 같아서

구조를 바꾸고 싶은데 되는건지 모르겠다




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
90
91
92
93
#include <string>
#include <vector>
#include <iostream>
 
using namespace std;
 
int solution(int temperature, int t1, int t2, int a, int b, vector<int> onboard) {
    int answer = 0;
    int near = 0;
    int temp = temperature;
  
    near = abs(temp - t1) < abs(temp - t2) ? t1 : t2;//t1, t2 중 근사값 찾기
 
    for (int i = 0; i < onboard.size(); i++)
    {
        if (onboard[i] == 1)//에어컨이 켜있을 때,
        {
 
            if (temp == near)
            {
                answer += b;//실내, 희망 온도가 같을 경우 b전력을 소비하여 유지
 
                continue;//이후 연산 무시하고 건너뛰기
            }
           
            temp < near ? temp++ : temp--;//실내 온도가 희망 온도와 다를 경우 a전력 소비하여 희망 온도와 가까워짐
            answer += a;
        }
        else//에어컨 껏을 때,
        {
           
            if (temp == temperature)//실내, 실외 온도가 다를 경우 1씩 실외 온도와 가까워짐
            {
                temp < temperature ? temp++ : temp--;
            }
        }
        
        
    }
 
    return answer;
}
 
void print(int temperature, int t1, int t2, int a, int b, vector<int> onboard, int result)
{
    cout.width(11);
    cout << temperature << " ";
 
    cout.width(3);
    cout << t1 << " ";
 
    cout.width(3);
    cout << t2 << " ";
 
    cout.width(3);
    cout << a << " ";
 
    cout.width(3);
    cout << b << " ";
 
    cout.width(6);
    cout << result << "     ";
    
    cout << "[";
    for (int i = 0; i < onboard.size(); i++)
    {
        cout << onboard[i] << ", ";
    }
    cout << "]" << endl;
 
 
}
 
void main()
{
    vector<int> list;
    int temp, t1, t2, a, b, answer;
    
   
    cout << "temperature  t1  t2   a   b result  onboard" << endl;
 
    list = { 0 ,0 ,1 ,1 ,1 ,1 ,1 };
    temp = 28; t1 = 18; t2 = 26; a = 10; b = 8
    answer = solution(temp, t1, t2, a, b, list);
    print(temp, t1, t2, a, b, list, answer);
 
    list = { 0 ,0 ,0 ,0 ,0 ,1 ,0 };
    temp = -10; t1 = -5; t2 = 5; a = 5; b = 1;
    answer = solution(temp, t1, t2, a, b, list);
    print(temp, t1, t2, a, b, list, answer);
 
 
}
cs


원래는 사람이 있을 때만 에어컨을 가동하고 없을 때는 끈 상태로 가정하고 코드를 짜고 있었다.

그런데 에어컨을 껏지만 승객이 타고 있는 경우가 있다.

그래서 입출력 예시에서는 에어컨이 켜있는지 꺼있는지 상태를 알 수 없기에

차라리 승객 탑승 유무를 버리고 0이 에어컨을 끈 상태, 1을 킨 상태로 하였음


결과




댓글 없음:

댓글 쓰기

c++ thread.h

 c++에서 쓰레드 돌릴려면 thread.h 헤더를 쓰면 되는데 이 친구는 쓰레드가 아직 실행 중인지, 아니면 강제 종료하거나 하는 함수가 없어서 조금 아쉬운 애다. std::thread 는 로컬 변수로 선언하든 new 동적 할당을 하든 start 함...