[SW Expert] #1860 진기의 최고급 붕어빵 :: 잡다한 프로그래밍
반응형

https://swexpertacademy.com/main/main.do

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

1. 해결방법

먼저 배열에 N개의 수를 입력받고 이를 정렬한 후, 늘어나는 시간과 비교하며 M시간이 되면 빵을 K개만큼 추가하고, 만약 배열의 값을 cnt로 두고 배열[cnt] == 시간일 경우 빵의 개수가 0보다 같거나 작으면 불가, 크면 빵의 개수를 줄이고 cnt를 1씩 늘리고 time도 1 늘리는 방법으로 해결하려 하였다


2. 오류

그러나 배열내부에 배열[0] == 3 배열[1] == 3같이 같은 값이 들어가 있을 경우 시간은 이미 늘어나버려서 무한루프에 빠지는 현상이 발생하였고 while문을 2번 돌리는 방법으로 다음과 같이 해결하였다. 더 쉬운 방법도 존재하는 것 같으니 다른 블로그도 참고하길 바란다.


3. 코드

#include<iostream>
#include<algorithm>
#include<string.h>

using namespace std;

string result = "Possible";
int tmp[101];
    int test_case;
	int T;
    int N;
    int M;
    int K;

void check(){
    int time = 0;
    int cnt = 0;
    int bread = 0;
	while(1){
        if(cnt == N) {break;}
        if((time % M) == 0 && time != 0) bread += K;
        
        while(1){
        if(tmp[cnt] == time){
         if(bread <=0){
             result = "Impossible";
             goto EXIT;
         }
         else{
          bread--;
          cnt++;
         }
      
        	}else{
         break;   
        }
        }
        
        time++;
    }

    EXIT:
    return;
}

int main(int argc, char** argv)
{
	scanf("%d",&T);
	for(test_case = 1; test_case <= T; ++test_case)
	{
        scanf("%d %d %d",&N ,&M ,&K);
		
        for(int i = 0; i < N; i++){
            scanf("%d", &tmp[i]);
        }
    
		sort(tmp, tmp+N);
        result = "Possible";
       check();
        
        printf("#%d %s\n", test_case ,result.c_str());
	}
	return 0;//정상종료시 반드시 0을 리턴해야합니다.
}

 

반응형

+ Recent posts