๐Ÿง‘โ€๐Ÿ’ป ๋ฐฑ์ค€ 11047

๐Ÿง‘โ€๐Ÿ’ป ๋ฐฑ์ค€ 11047

์ด์ „ ๊ธ€ ๐Ÿง‘โ€๐Ÿ’ป ๋ฐฑ์ค€ 11053 ๋ณด๋Ÿฌ๊ฐ€๊ธฐ.

์˜ค๋Š˜ ํ’€ ๋ฌธ์ œ๋Š” 11047๋ฒˆ, ๋™์ „ 0์ด๋‹ค.

N์ข…๋ฅ˜์˜ ๋™์ „์œผ๋กœ K์˜ ๊ฐ’์–ด์น˜๋ฅผ ๋งŒ๋“ค ๋•Œ ๊ฐ€์žฅ ์ ์€ ๋™์ „์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•œ๋‹ค. ๊ฐ€์žฅ ์ ์€ ๋™์ „ ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฏ€๋กœ ๊ฐ€์žฅ ํฐ ๊ฐ’์„ ๊ฐ–๋Š” ๋™์ „์„ ์ตœ๋Œ€ํ•œ ๋งŽ์ด ์‚ฌ์šฉํ•  ์ˆ˜๋ก ์œ ๋ฆฌํ•˜๋‹ค. ๋งˆ์นจ ๋™์ „์˜ ๊ฐ€์น˜๋Š” ์˜ค๋ฆ„์ฐจ์ˆœ์œผ๋กœ ์ž…๋ ฅ๋˜๊ธฐ ๋•Œ๋ฌธ์—, ๋งˆ์ง€๋ง‰์— ์ž…๋ ฅ๋œ ๋™์ „ ๋ถ€ํ„ฐ ๊ณ ๋ คํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ์ฒ˜์Œ์—” vector๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๋ ค๊ณ  ํ–ˆ์ง€๋งŒ, vector๋Š” push_back()ํ•จ์ˆ˜๋ฅผ ์ง€์›ํ•˜์ง€๋งŒ push_front()๋Š” ์ง€์›ํ•˜์ง€ ์•Š๊ธฐ๋•Œ๋ฌธ์— ์ž…๋ ฅ๋ฐ›์€ ๋™์ „์˜ ๊ฐ’์–ด์น˜๋ฅผ ๋’ค๋ถ€ํ„ฐ ํ™•์ธํ•ด์ค˜์•ผํ•œ๋‹ค. ํฐ ๋ฌธ์ œ๋Š” ์•„๋‹ˆ์ง€๋งŒ ์ด๊ฒŒ ๊ต‰์žฅํžˆ ๊ท€์ฐฎ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๊ณ , ๋˜ ํ‰์†Œ์— ์ž˜ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•˜๋˜ list๋ฅผ ์ด๋Ÿฐ ๋•Œ๋ผ๋„ ์‚ฌ์šฉํ•ด๋ณด๊ธฐ์œ„ํ•ด์„œ list๋ฅผ ์ด์šฉํ•ด๋ณด์•˜๋‹ค.

#include<iostream>
#include<list>

using namespace std;

int main(void){
    int numberOfValues;
    int targetValue;
    int best = 0;
    list<int> coinValue;

    cin>>numberOfValues;
    cin>>targetValue;

    for(int i = 0; i < numberOfValues; i++){
        int tempValue = 0;
        cin>>tempValue;
        coinValue.push_front(tempValue);
    }

    for(list<int>::iterator i = coinValue.begin(); i != coinValue.end(); ++i){
        int currentCoin = targetValue / *i;
        if(currentCoin > 0){
            best += currentCoin;
            targetValue -= ((*i) * currentCoin);
        }
    }

    if(targetValue == 0){
        cout<<best<<endl;
    }
    else{
        cout<<"error occured!"<<endl;
    }

    return 0;
}

๋™์ „์˜ ๊ฐ’์˜ ๊ฐœ์ˆ˜, ๋งž์ถฐ์•ผํ•˜๋Š” ๊ฐ’์–ด์น˜๋ฅผ ์ž…๋ ฅ๋ฐ›๊ณ , ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด ๋™์ „์˜ ๊ฐ€์น˜๋“ค์„ ์ž…๋ ฅ๋ฐ›์•„ list์— ๋„ฃ๋Š”๋‹ค. list๋Š” push_front()๋ฅผ ์ง€์›ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ทธ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ฐ’์–ด์น˜๊ฐ€ ํฐ ๋™์ „์ด ๋งจ ์•ž์— ์œ„์น˜ํ•˜๊ฒŒ๋œ๋‹ค. ์ดํ›„ iterator๋ฅผ ํ†ตํ•ด list๋ฅผ ์ˆœํšŒํ•˜๋ฉฐ ๊ฐ’์ด ํฐ ๋™์ „๋ถ€ํ„ฐ ๊ฐœ์ˆ˜๋ฅผ ์„ธ๊ณ  ๊ฐ’์„ ๊นŽ์•„๋‚ด๋ ค๊ฐ€๋ฉฐ ํ•„์š”ํ•œ ๋™์ „์˜ ๊ฐœ์ˆ˜๋ฅผ ๊ตฌํ•˜๋ฉด ๊ทธ๊ฒŒ ์ตœ์†Œ ๋™์ „์˜ ๊ฐœ์ˆ˜๊ฐ€ ๋œ๋‹ค. ํ˜น์‹œ๋ผ๋„ ๊ณ„์‚ฐ์ด ๋๋‚œ ํ›„์—๋„ ์ž”์•ก์ด ๋‚จ์„๊ฒฝ์šฐ๋ฅผ ๋Œ€๋น„ํ•ด ์ž”์•ก์ด 0์›์ด ์•„๋‹ˆ๋ฉด ์—๋Ÿฌ๋ผ๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•˜๋„๋ก ํ–ˆ๋‹ค.

ํ•ด๊ฒฐ
ํ’€์—ˆ๋‹ค!

๋‹ค์Œ ๊ธ€ ๐Ÿง‘โ€๐Ÿ’ป ๋ฐฑ์ค€ 11053 ๋ณด๋Ÿฌ๊ฐ€๊ธฐ.

Share: Twitter Facebook
Seunghun Yang's Picture

About Seunghun Yang

Seunghun is undergraduate student at Computer Science Engineering in CNU(Chungnam National University).

Daejeon, South Korea

Comments