你有 N 种配料,每种配料有酸度 S 和苦度 B 。用这些配料做成Perket时,总的酸度为所有配料酸度的乘积,总的苦度是所有配料苦度的和。你至少需要添加一种配料。
为了使口感适中,总的酸度和苦度之差的绝对值应该尽可能小,求这个最小值。
输入
第一行 1 个整数N (1≤N≤10) —— 配料的数量。
接下来 N 行每行 2 个整数Si和Bi
—— 每种配料的酸度和苦度。如果用所有配料来做Perket,总的酸度和苦度都 ≤10^9。
输出
N 行,每行 1 个整数 —— 所求的最小值。
Sample Input 1
1
3 10
Sample Output 1
7
Sample Input 2
2
3 8
5 8
Sample Output 2
1
Sample Input 3
4
1 7
2 6
3 8
4 9
Sample Output 3
1
思路,bfs枚举每种材料加或者不加所能得到的结果,选出最小的
#include<iostream>
#include<cstdio>
#include<iomanip>
#include<cstdlib>
#include <algorithm>
#include<string.h>
#include<queue>
#include<math.h>
#include<set>
#define ll long long
using namespace std;
struct peiliao{
ll s;
ll b;
};
peiliao pl[15];
int n;
ll minn=1e9+10;
bool vis[15];
void dfs(int i,ll x,ll y)//i用来代表在看第i种配料 ,s代表酸度,b代表苦度
{
if(i>n){
if(x==1&&y==0)return ;//一种都没加
minn=min(minn,abs(x-y));
return;
}
dfs(i+1,x*pl[i].s,y+pl[i].b);//加这种配料
dfs(i+1,x,y);//不加这种配料
}
int main()
{
cin >> n ;
for(int i=1;i<=n;i++)
{
cin >> pl[i].s >> pl[i].b ;
}
dfs(1,1,0);
cout << minn << endl ;
return 0;
}