본문 바로가기

하루하나코딩

백준 11047 : 동전 0 c++

코드

#include <iostream>

using namespace std;

int main(){
	
	int n, k;
	cin >> n >> k;
	int coin[11];
	
	for(int i = 0; i < n; i++){
		cin >> coin[i];	
	}
	
	int ans = 0;
	int total = 0;
	int j = n-1;
	while(total != k){
		total += coin[j];
		ans++;
		if(total > k){
			total -= coin[j];
			j--;
			ans--;
		}
	}
	
	cout << ans;
	
	return 0;
}

알게된 점

그리디로 풀면 된당!

오름차순으로 입력해주는게 참 좋다!

'하루하나코딩' 카테고리의 다른 글

백준 1890 : 점프 c++  (0) 2023.01.19
백준 1520 : 내리막 길  (0) 2023.01.17
백준 2133 : 타일채우기  (0) 2023.01.13
백준 2294 : 동전 2 c++  (0) 2023.01.13
백준 9461 : 파도반 수열 c++  (0) 2023.01.12