[백준] 11047 동전 0
#include <algorithm>
#include <vector>
#include <iostream>
using namespace std;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int n, k;
cin >> n >> k;
vector<int> coin;
for (int i = 0; i < n; ++i) {
int input;
cin >> input;
coin.push_back(input);
}
int cnt = 0;
int coinIdx = n - 1;
int sum = 0;
while (sum != k) {
while(sum + coin[coinIdx] > k) coinIdx--;
sum += coin[coinIdx];
cnt++;
}
cout << cnt;
return 0;
}
1년 전 코드
#include <iostream>
#include <vector>
using namespace std;
int main() {
vector <int> coin;
int n, k;
cin >> n >> k;
for (int i = 0; i < n; i++) {
int tmp;
cin >> tmp;
coin.push_back(tmp);
}
int cnt = 0;
int sum = 0;
while (sum!= k) {
if (sum + coin.back() <= k) {
sum += coin.back();
cnt++;
}
else coin.pop_back();
}
cout << cnt;
return 0;
}