Backward Digit Sums POJ - 3187

人一我百!人十我万!永不放弃~~~怀着自信的心,去追逐梦想
穷搜即可

  1. next_permutation求全排列,按照字典序顺序的;
  2. 每次求和和sum比较即可;求和的过程类似于杨辉三角形.
#include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <sstream>
#define mod 1000000007
#define eps 1e-6
#define ll long long
#define INF 0x3f3f3f3f
#define MEM(x,y) memset(x,y,sizeof(x))
#define Maxn 1005
using namespace std;
int a[Maxn];
int n;
int sum;

void solve()
{
	do
	{
		int b[Maxn];
		for(int i = 0; i < n; i++)
			b[i] = a[i];
		for(int i = n; i >= 2; i--)
		{
			for(int j = 0; j < i; j++)
			{
				b[j] = b[j] + b[j+1];
			}
		}
		int ans = b[0];
		if(ans == sum)
		{
			for(int i = 0; i < n; i++)
				printf("%d ", a[i]);
			break;
		}
	}while(next_permutation(a ,a+n));
}

int main()
{
	cin >> n >> sum;
	for(int i = 0; i < n; i++)
		a[i] = i + 1;
	solve();
	return 0;
}

上一篇:task02:leetcode.7,8,9


下一篇:POJ - 3126 Prime Path