| Time Limit: 1000MS | Memory Limit: 65536K | |
| Total Submissions: 4807 | Accepted: 2772 |
Description
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. Input
Output
Sample Input
4 16
Sample Output
3 1 2 4
Hint
Source
USACO 2006 February Gold & Silver
ac代码
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
int a[15],b[15];
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
int i,j;
for(i=1;i<=n;i++)
{
a[i]=i;
}
do{
for(i=1;i<=n;i++)
b[i]=a[i];
for(i=1;i<=n;i++)
{
for(j=1;j<=n-i;j++)
b[j]+=b[j+1];
}
if(b[1]==m)
break;
next_permutation(a+1,a+n+1);
}while(1);
for(i=1;i<=n;i++)
{
printf("%d%c",a[i],i==n?'\n':' ');
}
}
}POJ 题目Backward Digit Sums(next_permutation)
原文:http://blog.csdn.net/yu_ch_sh/article/details/44903969