发现问题一,全都不选的判断有错误,改进了一下
发现问题二,因为我是从0开始的,所以我要判断到x>n-1即可,不用到x>n
```cpp
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int n;
int st[3]={0}; //0代表不知道选不选,1代表选,2代表不选
int ta[20][2]; //开一个存 酸 苦 两种味道的二维数组
int res=1e9; //存答案
void dfs( int x ) {
if( x>n-1 ) {
bool use=false;
ta[15][0]=1; // 求出酸的积
ta[15][1]=0; // 求出苦的和
for( int i=0; i<n; i++ ) {
if(st[i]==1) { //选上就做运算
ta[15][0]*=ta[i][0];
ta[15][1]+=ta[i][1];
use=true;
}
}
if( use ) { //把最少的输出
res=min(res,abs(ta[15][0]-ta[15][1]));
}
return ;
}
//选这种调料
st[x]=1;
dfs(x+1);
st[x]=0;
//不选这种调料
st[x]=2;
dfs(x+1);
st[x]=0;
}
int main()
{
cin >> n;
for( int i=0; i<n; i++ ) {
for( int j=0; j<2; j++ ) {
cin >> ta[i][j];
}
}
dfs(0);
cout << res;
return 0;
}
```
@[mengxin_xiao_cai](/user/839774)
by mengxin_xiao_cai @ 2024-03-13 09:38:49
ok,问题解决,是由于st[x]的想法出现偏差,他开的不够大
by mengxin_xiao_cai @ 2024-03-13 09:47:03