使用的是递归算法,地址:http://acm.pku.edu.cn/JudgeOnline/problem?id=1564
#include <stdio.h>
#include <stdlib.h>
int cmp ( const void *a, const void *b )
{
return *( int * )b - *( int * )a;
}
int n;
int num[15];
int stack[15];
int flag;
void print ( int len, int s, int last )
{
int fa = -1;
for ( int i=s; i<n; i++ )
{
if ( last - num[i] == 0 && num[i] != fa )
{
stack[len] = num[i];
for ( int i=0; i<len; i++ )
{
printf ( "%d+", stack[i] );
}
printf ( "%d\n", stack[i] );
flag = 1;
}
else
{
if ( last - num[i] > 0 && num[i] != fa )
{
stack[len] = num[i];
print ( len+1, i+1, last-num[i] );
}
}
fa = num[i];
}
}
int main ()
{
int t;
while ( scanf ( "%d%d", &t, &n ) != EOF && ( t || n ) )
{
for ( int i=0; i<n; i++ )
{
scanf ( "%d", &num[i] );
}
qsort ( num, n, sizeof ( int ), cmp );
flag = 0;
printf ( "Sums of %d:\n", t );
print ( 0, 0, t );
if ( ! flag )
{
printf ( "NONE\n" );
}
}
return 0;
}