洛谷P2036 [COCI2008-2009#2] PERKET 题解(不用递归和用递归的区别。。)

 简单思路:n这么小,第一想到的就是暴力,于是就发生了这种事。。。。

#include
#include
using namespace std;
int n,sum,res=100000000;
struct sour_bitter
{
	int sour;
	int bitter;
}arr[12];
int main()
{
	
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> arr[i].sour >> arr[i].bitter;
	for (int i1 = 1; i1 <= n; i1++)
	{
		sum = abs(arr[i1].sour - arr[i1].bitter);
		if (sum < res)
			res = sum;
		for (int i2 = i1+1; i2 <= n; i2++)
		{
			sum = abs(arr[i1].sour * arr[i2].sour - (arr[i1].bitter + arr[i2].bitter));
			if (sum < res)
				res = sum;
			for (int i3 = i2+1; i3 <= n; i3++)
			{
				sum = abs(arr[i1].sour * arr[i2].sour*arr[i3].sour - (arr[i3].bitter+arr[i1].bitter + arr[i2].bitter));
				if (sum < res)
					res = sum;
				for (int i4 = i3+1; i4 <= n; i4++)
				{
					sum = abs(arr[i1].sour * arr[i2].sour * arr[i3].sour*arr[i4].sour- (arr[i4].bitter+arr[i3].bitter + arr[i1].bitter + arr[i2].bitter));
					if (sum < res)
						res = sum;
					for (int i5 = i4+1; i5 <= n; i5++)
					{
						sum = abs(arr[i1].sour * arr[i2].sour * arr[i3].sour * arr[i4].sour*arr[i5].sour - 
							(arr[i5].bitter+arr[i4].bitter + arr[i3].bitter + arr[i1].bitter + arr[i2].bitter));
						if (sum < res)
							res = sum;
						for (int i6 = i5+1; i6 <= n; i6++)
						{
							sum = abs(arr[i1].sour * arr[i2].sour * arr[i3].sour * arr[i4].sour * arr[i5].sour*arr[i6].sour -
								(arr[i6].bitter+arr[i5].bitter + arr[i4].bitter + arr[i3].bitter + arr[i1].bitter + arr[i2].bitter));
							if (sum < res)
								res = sum;
							for (int i7 = i6+1; i7 <= n; i7++)
							{
								sum = abs(arr[i1].sour * arr[i2].sour * arr[i3].sour * arr[i4].sour * arr[i5].sour * arr[i6].sour*arr[i7].sour -
									(arr[i7].bitter+arr[i6].bitter + arr[i5].bitter + arr[i4].bitter + arr[i3].bitter + arr[i1].bitter + arr[i2].bitter));
								if (sum < res)
									res = sum;
								for (int i8 = i7+1; i8 <= n; i8++)
								{
									sum = abs(arr[i1].sour * arr[i2].sour * arr[i3].sour * arr[i4].sour * arr[i5].sour * arr[i6].sour * arr[i7].sour*arr[i8].sour -
										(arr[i8].bitter+arr[i7].bitter + arr[i6].bitter + arr[i5].bitter + arr[i4].bitter + arr[i3].bitter + arr[i1].bitter + arr[i2].bitter));
									if (sum < res)
										res = sum;
									for (int i9 = i8+1; i9 <= n; i9++)
									{
										sum = abs(arr[i1].sour * arr[i2].sour * arr[i3].sour * arr[i4].sour * arr[i5].sour * arr[i6].sour * arr[i7].sour * arr[i8].sour*arr[i9].sour -
											(arr[i9].bitter+arr[i8].bitter + arr[i7].bitter + arr[i6].bitter + arr[i5].bitter + arr[i4].bitter + arr[i3].bitter + arr[i1].bitter + arr[i2].bitter));
										if (sum < res)
											res = sum;
										for (int i10 = i9+1; i10 <= n; i10++)
										{
											sum = abs(arr[i1].sour * arr[i2].sour * arr[i3].sour * arr[i4].sour * arr[i5].sour * arr[i6].sour * arr[i7].sour * arr[i8].sour * arr[i9].sour*arr[i10].sour -
												(arr[i10].bitter+arr[i9].bitter + arr[i8].bitter + arr[i7].bitter + arr[i6].bitter + arr[i5].bitter + arr[i4].bitter + arr[i3].bitter + arr[i1].bitter + arr[i2].bitter));
										}
									}
								}
							}
						}
					}
				}
			}
		}
	}
	cout << res;
	return 0;
}

为了节省代码,可以发现其实for中进行的操作都是相同的,所以采用递归会大大缩短代码量 

#include
#include
using namespace std;
int n,sum,res=100000000,x=1,y=0;
struct sour_bitter
{
	int sour;
	int bitter;
}arr[12];
void sousuo(int a)
{
	sum = abs(x - y);
	if (sum < res&&(x!=1||y!=0))
		res = sum;
	if (a > n)
		return;
	for (int i = a; i <= n; i++)
	{
		x *= arr[i].sour;
		y += arr[i].bitter;
		sousuo(i + 1);
		x /= arr[i].sour;
		y -= arr[i].bitter;
	}
}
int main()
{
	
	cin >> n;
	for (int i = 1; i <= n; i++)
		cin >> arr[i].sour >> arr[i].bitter;
	sousuo(1);
	cout << res;
	return 0;
}

你可能感兴趣的:(洛谷刷题,c++,算法)