求助!边界条件
查看原帖
求助!边界条件
655995
realyanhualengluo楼主2023/1/17 15:57

感觉总体应该问题不大,但是输出的时候所有的样例cha都是没变的

#include<iostream>
#include<cmath>
using namespace std;
struct food {
	int s;//酸度
	int b;//苦度
};
int n;
food arr[10];
int book[100];
long cha = 99999999;
//index是food数组里头的索引
void dfs(int index,int sums,int sumb) {
	//全部选了一遍
	
	if(index>=n){
		if (abs(sums-sumb )< cha) {
			cha = abs(sums - sumb);
		}
		return;
	}
	for (index; index < n; index++) {
		//不能重复选的话
		if (book[index] == 0) {
			//选了
			book[index] = 1;

			dfs(index++, sums * arr[index].s, sumb + arr[index].b);
			//不选
			book[index] = 0;
		}
		
	}
}
int main() {
	cin >> n;
	//放在数组1-n的位置上
	for (int i = 0; i <n; i++) {
		cin >> arr[i].s >> arr[i].b;
	}
	dfs(0, 1, 0);
	cout << cha;
}
2023/1/17 15:57
加载中...