Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- N번째큰수
- 백준
- 사이클 없는 그래프
- 1174
- hcpc
- 이분탐색 #dp #11053
- LIS #가장긴증가하는부분수열 #
- graph
- graph #최단경로
- 줄어드는수
- 코딩
- 16202
- 3D #Reconstruction #computer #vision #volume #metric #tsdf #kinect #fusion
- C++
- 레드아보
- 쌤쌤쌤
- 20117
- 진법변환 #2to10 #10to2 #이진법 #십진법 #변환 #bitset #c++
- 22869
- 최소 #공배수 #최대 #공약수 #유클리드 #호제법 #lcm #gcd #c++ #boj #3343 #백준 #장미
- 투포인터 #백준 #boj #20922 #22862
- 3343
- 30870
- BOJ
- backtracking #codetree #디버깅 #삼성코테
- 호반우 상인
- c++ #boj #
- boj #백준
- c++ #입출력 #속도 #ios #sync_with_stdio #cin #cout #tie
- 백준 #다익스트라 #dijkstra #9370 #c++
Archives
- Today
- Total
hyunjin
[C++][그리디 - 동전0 (11047)] for,stack 사용 본문
728x90
[소스 코드]
#include <bits/stdc++.h>
using namespace std;
int main(){
stack<int> s;
int N,K,result=0;
int tmp;
cin>>N>>K;
for(int i = 0 ; i < N ; i++){
cin>>tmp;
s.push(tmp);
}
tmp=K;
while(K > 0){
if(s.top() > K) {
s.pop();
continue;
}
result = result + K/s.top();
K = K%s.top();
s.pop();
}
cout<<result;
return 0;
}
[풀이 방법]
동전 단위들을 stack으로 입력받아 목표 금액보다 크면 pop하고
사용가능하면 사용한만큼 결과에 반영하고 차액에 대해 다시 반복한다.
어려운 문제는 아니였다.
9개월 후 다시 풀어봤다
왜 stack으로 푼건지, 쉬운 방법 두고
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
int n,k,ans=0;
cin>>n>>k;
int coin_type[n];
int i =n-1;
for(;i>=0;i--)cin>>coin_type[i];
i=0;
while(k){
ans+=(k/coin_type[i]);
k%=coin_type[i];
i++;
}
cout<<ans;
}
728x90
'알고리즘 연습 > 백준' 카테고리의 다른 글
[C++][DP - 1로 만들기(1463)] bottom - up (0) | 2021.02.02 |
---|---|
[C++][그리디-회의실배정(1931)] pair, 정렬 (0) | 2020.10.14 |
[C++][그리디 - ATM(11399)] (0) | 2020.09.08 |
[C++][그리디 - 설탕배달(2839)] 2차 풀이 (0) | 2020.09.04 |
[C++][수학2/골드바흐의 추측(9020)] 소수 (0) | 2020.08.31 |