http://acm.pku.cn/JudgeOnline/problem?id=3187
Source Code
Problem: 3187 User: Torres
Memory: 404K Time: 94MS
Language: G++ Result: Accepted
Source Code
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
int main()
{
int N,S,a[10],b[10];
int i,j;
scanf("%d%d",&N,&S);
for(i=0;i<N;i++) a[i]=i+1;
do
{
memcpy(b,a,sizeof(a));
j=N;
while(j--)
{
for(i=0;i<j;i++)
b[i]+=b[i+1];
}
if(b[0]==S) break;
}while(next_permutation(a,a+N));
for(i=0;i<N;i++)
printf("%d ",a[i]);
printf("\n");
return 0;
}
http://acm.pku.cn/JudgeOnline/problem?id=3187题意很好理解,看样例便可以懂,又一次用STL偷懒了,不过在N很小的情况下,很方便。