The question is relatively simple, not to mention, that is, to put the rectangle in the box according to the question (the maximum width of the box is given). If you cannot put it down, put down a line. Note that it cannot be rotated here, which is the reason for the simulation of water questions, and it is not allowed to find the maximum or minimum, that is, to simulate the final state. It cannot be rotated and can be placed directly.
Code:
# Include <stdio. h> # include <stdlib. h> # include <string. h> # define min (a, B) (a <B? A: B) # define max (A, B) (A> B? A: B) int maxwidth; int main () {While (scanf ("% d", & maxwidth) {int W, H; int wnow = 0, hnow = 0; // the width and height of all items in the current box, initially 0int hmax = 0; // The maximum int wmax = 0 for each row; // The widest value of all rows while (1) {scanf ("% d", & W, & H ); if (W =-1 & H =-1) {break;} If (W> maxwidth-wnow) // if the current part cannot be placed in the current row, then, you can only put it in the next row {Hnow + = hmax; // so high will increase the maximum value of the previous row // note that this block will become the first wnow = W in the next row; // The current row width is the block width hmax = H; // the maximum height of the row is initialized as the block height} else // If the row can be placed, then this block is placed in the row {hmax = max (hmax, H); // the maximum value of the row is updated wnow + = W; // The current width value is updated wmax = max (wmax, wnow); // obtain the widest value of all rows before the current block} printf ("% d x % d \ n", wmax, Hnow + hmax ); // output the widest value of all rows, and add hmax} return 0;} to the previous Hnow row ;}