POJ 3187 Backward Digit Sums

作业题,用STL穷举过了,题目也叫穷举法 - -!

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;

const int maxn = 11;

int n, sum;

int A[maxn];

int check(int *A, int num)
{
	int sa[maxn];
	for(int i = 0; i < num; i++) sa[i] = A[i];
	for(;;)
	{
		if(num == 0)
		{
			if(sa[0] == sum) return 1;
			else return 0;
		}
		for(int i = 0; i < num-1; i++) sa[i] = sa[i] + sa[i+1];
		num--;
	}
	return 0;
}

void solve()
{
	for(int i = 0; i < n; i++) A[i] = i+1;
	do
	{
		if(check(A, n))
		{
			for(int i = 0; i < n; i++) printf(i != n-1? "%d ":"%d\n", A[i]);
			return ;
		}
	}while(next_permutation(A, A+n));
}

int main()
{
	while(~scanf("%d%d", &n, &sum))
	{
		solve();
	}
	return 0;
}


你可能感兴趣的:(POJ 3187 Backward Digit Sums)