Given two numbers, the length of these two numbers cannot exceed 6 digits. Then, how can we determine the split of the last number to bring the value closer to the previous one, in fact, this problem is equivalent to inserting a board in the next digit. You can use state compression to enumerate the partitions.
The Code is as follows:
# Include <iostream> # include <cstdlib> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # include <string> # include <map> # include <queue> # include <vector> # include <stack> # include <list> # include <set> using namespace STD; int T, num, bit [10], idx; void break () {idx =-1; while (Num) {bit [++ idx] = num % 10; num/= 10 ;}for (INT I = 0, j = idx; I <j; ++ I, -- j) {swap (bit [I], bit [J]) ;}} Int get (int A, int B) {int ret = 0; For (INT I = A; I <B; ++ I) {RET * = 10, RET + = bit [I];} return ret;} void print (INT state) {int last = 0; For (INT I = 1; I <= idx + 1; ++ I) {If (State & (1 <I) {printf ("% d", get (last, I); last = I ;}}} bool judge (INT state, Int & RET) {int last = 0; ret = 0; For (INT I = 1; I <= idx + 1; ++ I) {If (State & (1 <I) {RET + = get (last, I); last = I ;}} Return ret <= T;} int main () {int mask, Max, CNT, State; while (scanf ("% d", & T, & num ), T | num) {int I; If (t = num) {printf ("% d \ n", T, num); continue ;} mask = CNT = 0; max = 0; break (); for (I = 1; I <= idx; ++ I) {mask | = (1 <I ); // assign a value to the mask} for (I = 0; I <= mask; I ++ = 2) {int T = I; T | = (1 <(idx + 1); int ret; If (Judge (T, RET) {If (max <RET) {max = ret; state = T; CNT = 1;} Else if (max = RET) ++ CNT;} If (max = 0) puts ("error"); else if (CNT! = 1) puts ("REJECTED"); else {printf ("% d", max); print (State); puts ("") ;}} return 0 ;}