POJ 3187 Backward Digit Sums(next_permutation)

Description
FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N <= 10) in a certain order and then sum adjacent numbers to produce a new list with one fewer number. They repeat this until only a single number is left. For example, one instance of the game (when N=4) might go like this: 
    3   1   2   4

      4   3   6

        7   9

         16
Behind FJ's back, the cows have started playing a more difficult game, in which they try to determine the starting sequence from only the final total and the number N. Unfortunately, the game is a bit above FJ's mental arithmetic capabilities. 
Write a program to help FJ play the game and keep up with the cows.
Input
Line 1: Two space-separated integers: N and the final sum.
Output
Line 1: An ordering of the integers 1..N that leads to the given sum. If there are multiple solutions, choose the one that is lexicographically least, i.e., that puts smaller numbers first.
Sample Input
4 16

Sample Output
3 1 2 4

강 한 STL, 내 가 시합 할 때 DFS 가 했 어.
제목: 1 ~ n 배열 작업 의 값 은 sum 입 니 다.  
next_permutation 의 용법: 사전 순서에 따라 전체 배열 을 출력 하고 false 를 되 돌려 주지 않 았 습 니 다.
DFS 버 전:
#include
#include
#include
#include
#include
typedef long long LL;
using namespace std;
int a[15],f[15];
int n,sum;
int solve()
{
    int s=0;
    for(int i=0;i=1;j--)
              f[j]+=f[j-1];//    
       }
       while(solve()!=sum)
          next_permutation(a,a+n);
       for(int i=0;i

next_permutation:
POJ 1146:
#include
#include
#include
#include
#include
#include
typedef long long LL;
using namespace std;
int num[15],a[15];
int visit[15],p[15];
int n,sum,flag;
int solve(int k)
{
     for(int i=1;i<=n;i++)
        p[i]=num[i];
     while(k>1)
     {
         for(int i=1;i

POJ 1731 :
#include
#include
#include
#include
#include
typedef long long LL;
using namespace std;
int main()
{
 int len;
 char s[51];
 while(scanf("%s",s)!=EOF)
 {
  if(s[0]=='#')
   break;
  len=strlen(s);
  if(next_permutation(s,s+len))
   printf("%s
",s); else printf("No Successor
"); } return 0; }

좋은 웹페이지 즐겨찾기