관리 메뉴

기억을 위한 기록들

코딩 게임 - The Gift (C++) 본문

Coding Test - cpp

코딩 게임 - The Gift (C++)

에드윈H 2020. 7. 27. 19:35

 

 

 

 

#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;


int main(){
    int N; //인원
    cin >> N; cin.ignore();

    int C; //비용
    cin >> C; cin.ignore();

    int arr[N];
    int result[N];
    int SumMoney=0;

    for (int i = 0; i < N; i++) {
        int B;
        cin >> B;
        
        arr[i]=B;
        SumMoney+=B;
        cin.ignore();
    }

    int RemainMoney=C;
    int RemainPeople=N;

    if(SumMoney<C){
      cout << "IMPOSSIBLE" << endl;
    }else{
        int Giftavg=C/N;
        int tempValue=0;

        for(int i=0;i<N;i++){ //버블정렬
            int temp=0;
            for(int j=0;j<N-1-i;j++){
                if(arr[j]>arr[j+1]){
                    temp=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
            }
        }

        for(int i=0;i<N;i++){
            int curMoney=arr[i];
            int avgMoney=RemainMoney/RemainPeople;

            if(curMoney<avgMoney){ 
                RemainMoney-=curMoney;
                result[i]=curMoney;
            }else{
                RemainMoney-=avgMoney;
                result[i]=avgMoney;
            }
            RemainPeople--;      
        }
        
        if(RemainMoney>0)
            result[N-1]+=RemainMoney;
        
        for(int i=0;i<N;i++){
            cout<<result[i]<<endl;
        }

    }

	return 0;
}