πŸ•οΈ ICPC Sinchon/Greedy

[λ°±μ€€][C++/Cμ–Έμ–΄] 5585번 : κ±°μŠ€λ¦„λˆ

선달 2021. 2. 8. 06:54
λ°˜μ‘ν˜•
μ‹œκ°„μ΄.. 정말.. μ˜€λž˜κ±Έλ Έλ‹€....... λ‹€μ§œκ³ μ§œ κ·Έλ¦¬λ””μ•Œκ³ λ¦¬μ¦˜ ν’€κ² λ‹€κ³  λ°±μ€€ λͺ©λ‘λ§Œλ“€κ³ ,, μ €μ‰¬μš΄κ±°λΆ€ν„° λ„μ „ν•˜λŠ”λ° μ•ˆν’€λ €μ„œ γ„Ήγ…‡ λ„ˆλ¬΄ μ†μƒν–ˆμŒ,, μ•„λ¬΄κ±°λ‚˜ 이쀑 ν•˜λ‚˜ ν’€κ³  μžκ² λ‹€κ³  마음먹음 = λ°€μƒ˜ γ…‹γ…‹γ…‹γ…‹γ…‹γ…‹ ν•˜λ‚˜ν’€λ‹€κ°€ λ°€μƒ˜ γ… γ… 

1λ…„λ§Œμ— λŒμ•„μ™€μ„œ λ‹€μ‹œ ν’€μ—ˆλ‹€.

https://www.acmicpc.net/problem/5585

 

5585번: κ±°μŠ€λ¦„λˆ

νƒ€λ‘œλŠ” 자주 JOIμž‘ν™”μ μ—μ„œ 물건을 μ‚°λ‹€. JOIμž‘ν™”μ μ—λŠ” μž”λˆμœΌλ‘œ 500μ—”, 100μ—”, 50μ—”, 10μ—”, 5μ—”, 1엔이 μΆ©λΆ„νžˆ 있고, μ–Έμ œλ‚˜ κ±°μŠ€λ¦„λˆ κ°œμˆ˜κ°€ κ°€μž₯ 적게 μž”λˆμ„ μ€€λ‹€. νƒ€λ‘œκ°€ JOIμž‘ν™”μ μ—μ„œ 물건을 사

www.acmicpc.net

문제

νƒ€λ‘œλŠ” 자주 JOIμž‘ν™”μ μ—μ„œ 물건을 μ‚°λ‹€. JOIμž‘ν™”μ μ—λŠ” μž”λˆμœΌλ‘œ 500μ—”, 100μ—”, 50μ—”, 10μ—”, 5μ—”, 1엔이 μΆ©λΆ„νžˆ 있고, μ–Έμ œλ‚˜ κ±°μŠ€λ¦„λˆ κ°œμˆ˜κ°€ κ°€μž₯ 적게 μž”λˆμ„ μ€€λ‹€. νƒ€λ‘œκ°€ JOIμž‘ν™”μ μ—μ„œ 물건을 사고 μΉ΄μš΄ν„°μ—μ„œ 1000μ—” 지폐λ₯Ό ν•œμž₯ λƒˆμ„ λ•Œ, 받을 μž”λˆμ— ν¬ν•¨λœ μž”λˆμ˜ 개수λ₯Ό κ΅¬ν•˜λŠ” ν”„λ‘œκ·Έλž¨μ„ μž‘μ„±ν•˜μ‹œμ˜€.

예λ₯Ό λ“€μ–΄ μž…λ ₯된 예1의 κ²½μš°μ—λŠ” μ•„λž˜ κ·Έλ¦Όμ—μ„œ 처럼 4개λ₯Ό 좜λ ₯ν•΄μ•Ό ν•œλ‹€.

μž…λ ₯

μž…λ ₯은 ν•œμ€„λ‘œ μ΄λ£¨μ–΄μ Έμžˆκ³ , νƒ€λ‘œκ°€ μ§€λΆˆν•  돈(1 μ΄μƒ 1000미만의 μ •μˆ˜) 1κ°œκ°€ μ“°μ—¬μ Έμžˆλ‹€.

좜λ ₯

μ œμΆœν•  좜λ ₯ νŒŒμΌμ€ 1ν–‰μœΌλ‘œλ§Œ λ˜μ–΄ μžˆλ‹€. μž”λˆμ— ν¬ν•¨λœ 맀수λ₯Ό 좜λ ₯ν•˜μ‹œμ˜€.

 

풀이

// Authored by : seondal
// Co-authored by : -

// #include <bits/stdc++.h>
#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

int main() {
    ios_base::sync_with_stdio(false); cin.tie(NULL);
    
    int n;
    cin >> n;
    
    int charge = 1000 - n;
    int cnt = 0;
    while(charge > 0) {
        if(charge >= 500)
            charge -= 500;
        else if(charge >= 100)
            charge -= 100;
        else if(charge >= 50)
            charge -= 50;
        else if(charge >= 10)
            charge -= 10;
        else if(charge >= 5)
            charge -= 5;
        else if(charge >= 1)
            charge -= 1;
        
        cnt++;
    }
    
    cout << cnt;
    
    return 0;
}

/*
 */

 

1λ…„ μ „μ˜ 풀이

#include <stdio.h>

int main(){
    int n;
    scanf("%d",&n);
    int ch = 1000-n;
    int x=0;
    
    for(; ch>=500;){
        ch = ch - 500;
        x++;
    }
    for(; ch>=100;){
        ch = ch - 100;
        x++;
    }
    for(; ch>=50;){
        ch = ch - 50;
        x++;
    }
        for(; ch>=10;){
        ch = ch - 10;
        x++;
    }
        for(; ch>=5;){
        ch = ch - 5;
        x++;
    }
        for(; ch>=1;){
        ch = ch - 1;
        x++;
    }
    
    printf("%d",x);
}
λ°˜μ‘ν˜•