본문 바로가기

Problem Solving/탑코더 알고리즘 트레이닝

1. 시뮬레이션

반응형

시뮬레이션: 

초기 상태와 어떤 작업을 수행할지 제공하고 최종 결과가 어떻게 될지 답하는 문제.

주어진 처리를 수행하기만 하면 되는 간단한 내용.

이런 과정을 거쳐 나온 결과가 무엇인가?

과정에 따라 코드를 작성하면 된다.

수행해야 하는 과정이 모두 나와 있는 문제



키위 주스 문제

타로는 맛있는 키위 주스를 준비했습니다.
타로는 0부터 N-1이라 이름을 붙인 N개의 병에 키위 주스를 넣었습니다.
이때 i번째의 병의 용량은 capacities[i] 리터이며 타로가 i번째 병에 넣은 키위 주스의 양을 bottles[i] 리터라고 합니다.

타로는 병에 키위 주스를 재분배하려고 하며, 0부터 M-1까지 M회 조작합니다.
i번쨰의 조작은 타로가 병 fromId[i]부터 병 toId[i]에 키위 주스를 넣는 것을 의미합니다.
병 fromId[i]가 비어 있거나 병 toId[i]가 꽉 차 있는 순간, 타로는 더 이상 키위 주스를 넣지 않습니다.

N개의 요소를 가진 정수 배열 int[]를 리턴해주세요.
배열의 i번째 요소는 모든 주스를 쏟는 작업이 완료되고 i번째 병에 남아 있는 키위 주스의 양입니다.

입력

capacities : 2~50개의 요소가 있는 배열, 각 요소는 1 <= N <= 1000000 사이의 값을 갖는다.
bottles : capacities와 같은 수의 요소가 있는 배열. bottles[i]는 capacities[i]에 들어있는 주스의 양을 의미
fromId : 1~50개의 요소가 있는 배열
toId : fromId와 같은 수의 요소가 있는 배열

출력

병들에 남아있는 주스의 양을 담고 있는 배열

IO Example


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
 public class KiwiJuiceEasy1 {
    public static void main(String[] args) {
        int[] capacities=new int[]{10,10,10};//전체 병의 크기
        int[] bottles=new int[]{3,7,3};//현재들어있는 용량
        int[] fromld=new int[]{1,0,1,0};//옮기고 싶은곳의 병
        int[] told=new int[]{0,1,0,1};//옮김을 받아야하는 곳의 병
        
        int[] a = thePouring(capacities,bottles,fromld,told);
        
        for(int i=0; i<a.length; i++) {
            System.out.println(a[i]);
        }
}
    
    public static int[] thePouring(int[] capacities, int[] bottles, int[] fromld, int[] told){
        for (int i = 0; i < fromld.length; i++){
             int from = fromld[i];
             int to = told[i];
             int toSpace = capacities[to] - bottles[to];//옮길려는곳의 남은공간
             
             if (toSpace >= bottles[from]) { //옮기려는 양이 넘치지 않는다면
                 bottles[to] += bottles[from];
                 bottles[from] -= bottles[from];//병을 비우자
             }else{
                 bottles[to] += toSpace;
                 bottles[from] -= toSpace;
             }
        }
             return bottles;
}
}
 
cs


첫번째

옮기기전의 병과 옮기려는 병의 양의 비교를 통해서 섞어주었다.

반복문과 조건문을 활용하였다.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public static int[] thePouring(int[] capacities, int[] bottles, int[] fromld, int[] told){
        for (int i = 0; i < fromld.length; i++){
             int from = fromld[i];
             int to = told[i];
            
             int vol = Math.min(bottles[from], capacities[to]-bottles[to]);//최소값을 구해서
             
             bottles[from] -= vol; //빼주고
             bottles[to] += vol;  //더해주자
        }
             return bottles;
}
}
}
 
cs


두번째

조건문을 활용하지않고 최소값 min 메소드를 통해 구했다.

조건문보다는 복잡하지 않을수 있었다.


1
2
3
4
5
6
7
8
9
10
11
public static int[] thePouring(int[] capacities, int[] bottles, int[] fromld, int[] told){
    
        for (int i = 0; i < fromld.length; i++){
            
            int sum = bottles[fromld[i]] + bottles[told[i]];
            bottles[told[i]] = Math.min(sum,capacities[told[i]]);
            bottles[fromld[i]] = sum-bottles[told[i]];
            
        }
             return bottles;
}
cs


세번째

이것도 조건문을 활용하기보다도

전체량 sum과 최소값을 활용하였다.




반응형

'Problem Solving > 탑코더 알고리즘 트레이닝' 카테고리의 다른 글

2. 전체탐색(암호)  (0) 2019.01.31
2. 전체 탐색(즐거운 파티)  (0) 2019.01.30