poj1416

简单题

View Code
   
     
#include < iostream >
#include
< cstdio >
#include
< cstdlib >
#include
< cstring >
using namespace std;

int n, m, a[ 10 ], la, ans;
bool reject;
bool split[ 10 ], ansplit[ 10 ];

void work()
{
la
= 0 ;
while (n > 0 )
{
a[la]
= n % 10 ;
n
/= 10 ;
la
++ ;
}
for ( int i = 0 ; i < la / 2 ; i ++ )
swap(a[i], a[la
- i - 1 ]);
}

void dfs( int pos, int sum, int d)
{
if (sum + d > m)
return ;
if (pos == la)
{
sum
+= d;
if (ans == sum)
{
reject
= true ;
return ;
}
if (sum <= m && sum > ans)
{
reject
= false ;
ans
= sum;
for ( int i = 0 ; i < la; i ++ )
ansplit[i]
= split[i];
}
return ;
}
dfs(pos
+ 1 , sum, d * 10 + a[pos]);
split[pos]
= true ;
dfs(pos
+ 1 , sum + d, a[pos]);
split[pos]
= false ;
}

void output()
{
if (ans == - 1 )
{
printf(
" error\n " );
return ;
}
if (reject)
{
printf(
" rejected\n " );
return ;
}
printf(
" %d " , ans);
for ( int i = 0 ; i < la; i ++ )
{
if (ansplit[i])
putchar(
' ' );
printf(
" %d " , a[i]);
}
printf(
" \n " );
}

int main()
{
// freopen("t.txt", "r", stdin);
while (scanf( " %d%d " , & m, & n), n | m)
{
work();
memset(split,
0 , sizeof (split));
ans
= - 1 ;
reject
= false ;
dfs(
1 , 0 , a[ 0 ]);
output();
}
return 0 ;
}

你可能感兴趣的:(poj)