[ 문제 ]
[ 접근방법 ]
n이 최대 10 이므로 백트래킹으로도 충분히 가능하다.
재료 사용 여부를 비트마스크를 통해 처리했다.
[ 소스코드 ]
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int n;
vector<pair<int, int>> taste;
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cin >> n;
taste.resize(n);
for(int i = 0; i < n; i++){
cin >> taste[i].first >> taste[i].second;
}
int ans = 1e9;
for(int i = 1; i < (1 << n); i++){
int a = 1, b = 0;
for(int j = 0; j < n; j++){
if(i & (1 << j)){
a *= taste[j].first;
b += taste[j].second;
}
}
ans = min(ans, abs(a - b));
}
cout << ans;
return 0;
}
'PS' 카테고리의 다른 글
[ 백준 / C++ ] 11501 : 주식 (1) | 2024.12.11 |
---|---|
[ 백준 / C++ ] 4659 : 비밀번호 발음하기 (0) | 2024.12.10 |
[ 백준 / C++ ] 1495 : 기타리스트 (0) | 2024.12.05 |
[ 백준 / C++ ] 2304 : 창고 다각형 (0) | 2024.12.04 |
[ 백준 / C++ ] 5397 : 키로거 (0) | 2024.11.28 |