# Include <stdio. h>
# Include <stdlib. h>
Typedef struct {
Int * W;
Int C;
Int CW;
Int bestcw;
Int N;
} Ship_info;
Ship_info ship;
Void backtrace (int I );
Void backtrace (int I)
{
If (I> SHIP. N) return;
If (I = ship. N ){
If (ship. CW> SHIP. bestcw ){
Ship. bestcw = ship. CW;
}
}
If (ship. CW + ship. W [I] <ship. c ){
Ship. CW + = ship. W [I];
Backtrace (I + 1 );
Ship. CW-= ship. W [I];
}
Backtrace (I + 1 );
}
Int main ()
{
Int temp [5] = {20, 15, 5, 2, 30 };
Ship. W = temp;
Ship. c = 55;
Ship. CW = 0;
Ship. bestcw = 0;
Ship. n = 5;
Backtrace (0 );
Printf ("% d", ship. bestcw );
System ("pause ");
Return 0;
}
####################################### Non-recursion
# Include <stdio. h>
# Include <stdlib. h>
Typedef struct {
Int * W;
Int C;
Int CW;
Int bestcw;
Int N;
Int * X;
} Ship_info;
Ship_info ship;
Void backtrace (int I );
Void backtrace (int I)
{
While (1 = 1 ){
While (I <ship. N ){
If (ship. CW + ship. W [I] <ship. c ){
Ship. X [I] = 1;
Ship. CW + = ship. W [I];
}
Else {
Ship. X [I] = 0;
}
I ++;
};
If (I> = ship. N ){
If (ship. CW> SHIP. bestcw ){
Ship. bestcw = ship. CW;
}
}
I --;
While (I> 0 & ship. X [I] = 0 ){
I --;
}
If (I = 0) return ship. bestcw;
If (ship. X [I]> 0 ){
Ship. CW-= ship. W [I];
Ship. X [I] = 0;
}
I ++;
}
}
Int main ()
{
Int temp [5] = {20, 15, 5, 2, 30 };
Int temp1 [5] = {0, 0, 0, 0, 0 };
Ship. W = temp;
Ship. x = temp1;
Ship. c = 55;
Ship. CW = 0;
Ship. bestcw = 0;
Ship. n = 5;
Backtrace (0 );
Printf ("% d", ship. bestcw );
System ("pause ");
Return 0;
}