티스토리 뷰

반응형


#. Problem


* The copyright in this matter is in BOJ



#. Resolution Process

  1. Read and understand problem

  2. Redefine the problem + abstract

  3. Create solution plan (select Algorithm, Data structure)

  4. Prove the plan (check performance time and usage memory)

  5. Carry out the plan

  6. Look back on the plan and find a way to improve it


#. Solve


#. Python

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cow = []
pos = 0
 
for i in range(3):
    a = list(map(int, input().split()))
    cow.append(a)
 
for i in range(100):
    pos = i % 3
    nxt = (i + 1) % 3
 
    cow[nxt][1], cow[pos][1= min(cow[nxt][0], cow[nxt][1+ cow[pos][1]), \
                               max(cow[pos][1- (cow[nxt][0- cow[nxt][1]), 0)
 
for i in range(3):
    print(cow[i][1])
cs


이차원 리스트를 사용해서 그런지 약간 복잡시럽게 보이긴 하네.. 하핳


#. Other Python Code

1
2
3
4
5
6
7
8
9
10
11
C, M = [000], [000]
 
for i in range(3):
    C[i], M[i] = map(int, input().split())
 
for i in range(100):
    now, nxt = i % 3, (i + 1) % 3
    M[now], M[nxt] = max(M[now] - (C[nxt] - M[nxt]), 0), min(C[nxt], M[nxt] + M[now])
 
for i in M:
    print(i)
cs


양동이와 우유의 양을 각각 다른 배열로 사용하여 코드가 간결해졌다.


line 6~8) 100회 타설 작업을 하는 과정

line 7) 현재 idx와 다음 idx를 저장. 

line 8) 동시에 처리해주면서 코드가 더 간결해질 수 있다.

   현재 idx의 우유 양은 다음 idx에 넣을 수 있는 우유의 양으로 연산해준다.

       다음 idx에 넣을 수 있는 우유 양이 현재 idx의 우유 양보다 클 경우 음수가 되어 현재 idx는 0이 되고

   다음 idx의 우유 양은 양동이 크기와 현재 idx 우유 양을 더한 양의 최솟값을 저장하면 된다.


굉장히 단순하게 풀 수 있는데 처음에 너무 복잡하게 생각해서 잘 안 풀렸었다..

가장 단순한 구현 문제들도 많이 풀어보면서 간결하게 코드를 짜내는 능력을 좀 키워야겠다..


#. 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
#include <cstdio>
#include <algorithm>
 
using namespace std;
 
const int N = 3;
int C[N], M[N];
 
int main(void)
{
    int i, now, nxt;
 
    for (i = 0; i < N; i++)
        scanf("%d %d"&C[i], &M[i]);
    
    for (i = 0; i < 100; i++)
    {
        now = i % 3;
        nxt = (i + 1) % 3;
 
        int tmp = max(M[now] - (C[nxt] - M[nxt]), 0);
        M[nxt] = min(C[nxt], M[nxt] + M[now]);
        M[now] = tmp;
    }
 
    for (i = 0; i < 3; i++)
        printf("%d\n", M[i]);
    
    return 0;
}
cs


#. Other C++ Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <stdio.h>
int c[5], m[5];
 
int main(){
    for(int i=0; i<3; i++)    
        scanf("%d%d"&c[i], &m[i]);
    
    for(int i=0; i<100; i++){
        if(c[(i+1)%3]-m[(i+1)%3]>=m[i%3]){
            m[(i+1)%3]+=m[i%3];
            m[i%3]=0;
        }
        else{
            m[i%3]-=c[(i+1)%3]-m[(i+1)%3];
            m[(i+1)%3]=c[(i+1)%3];
        }
    }
    
    printf("%d\n%d\n%d", m[0], m[1], m[2]);
}
cs


처음이 이 방법으로 풀었었는데, 계속 중간에 꼬였었다..


line 14~15 부분이 엉켰었던 것 같은데,

현재 idx의 우유 양은 다음 idx에 넣을 수 있는 우유 양을 빼주면 되고,

다음 idx의 우유 양은 어차피 꽉 차게 될 것이므로 양동이 크기로 저장해주면 되었다.




반응형
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday