20分请求助,谢谢大佬
查看原帖
20分请求助,谢谢大佬
839774
mengxin_xiao_cai楼主2024/3/12 22:51
#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( st[0]==1&&st[1]==1&&st[2]==1 ) {    //要求至少为一种,若都不选直接过 
		return ;
	}
	if( x>n ) {
		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];
			}
		}
		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;
}
2024/3/12 22:51
加载中...