Steel cutting problems, Steel Cutting

Source: Internet
Author: User

Steel cutting problems, Steel Cutting

The total length, number of orders, and length of each order are known. select a group of orders from the order to cut and process the steel, so that the steel can be used best, steel with fixed length is lost each time.

Let's write down my ideas. At first I didn't want to understand how to use recursion. However, after reading their code, I went through it to understand it, in fact, the idea is not very good, but it is easier to implement it.

Suppose we have a piece of steel with a length of 12 meters, of which 3 orders require a length of 5, 6, and 9 meters respectively. Each cut will have a loss of 2 meters, obtain the optimal order combination.

Now let's think about our normal ideas:
If there is only one order, the order of 9 meters is the most suitable, plus 2 meters of consumables, a total of 11 meters.

If there are two orders, there will be a combination of 5, 6, 5, 9, 6, 9. Obviously, these three combinations have exceeded the length of 12 meters, because if it is 5, 6, although the sum of orders is 11, there are still 4 meters of consumables for two cutting operations. The total size is 15 meters, which is far beyond the steel length.

As we can see above, the combination of the three orders will be even worse.

So how should we achieve this? We have an array to record whether the order is selected. Please refer to the figure below:

During initialization, the program starts to run in the unselected status. Select 5 first, and then select 6 if the length of the loss is less than 12, in this case, the loss length is greater than 12, and the 6 State is set to unselected. Then, the 9, 5, and 9 plus the loss length are selected, which is obviously more than 12, then, set the status of 9 to unselected. Then, the traversal from 5 is completed, and the status of 5 is set to unselected. Select 6 and then select from 5, so proceed ....

Of course, there must be two variables in the middle to record the best length and the best order combination.

The following code is attached:

# Include <stdio. h>/*** total length of known steel, order number, and length required for each order * The preparation procedure selects a group of orders from the order for steel cutting, * The best application of steel products is agreed, each cut loss * fixed length steel */# define N 20 # define DELTA 2 // cut steel loss/** best length **/int bestlen; /** selected order with the best length **/int bestsele [N];/** selected order, used to select **/int sele [N]; /** order with n **/int n;/** length of steel required for the Order **/int orderlen [N]; /** total steel length **/int total; void attempt (); int main (void) {int j; // obtain the total length of steel printf ("Please enter the length of the steel: \ N "); scanf (" % d ", & total); // obtain the steel Order number printf (" Please enter the number of the orders: \ n "); scanf ("% d", & n); // obtain the length required by each order number printf ("Please enter the length of every order: \ n "); for (j = 0; j <n; j ++) scanf ("% d", & orderlen [j]); // initialization, make all orders not selected for (j = 0; j <n; j ++) bestsele [j] = sele [j] = 0; // initialize the optimal length, set to 0 bestlen = 0; // obtain the optimal length attempt (); printf ("order: \ n"); for (j = 0; j <n; j ++) {if (bestsele [j]) print F ("% d \ t", orderlen [j]);} printf ("\ nlength: \ n % d", bestlen); return 0;} void attempt () {int I, len; // obtain the total length (plus loss) of the selected order for (len = I = 0; I <n; I ++) if (sele [I]) len + = (orderlen [I] + DELTA); if (len-DELTA <= total) {// note, the last order may not require loss if (bestlen <len) {bestlen = len; for (I = 0; I <n; I ++) bestsele [I] = sele [I];} // after each attempt to select an order, you need to restore it to the unselected status for (I = 0; I <n; I ++) {if (! Sele [I]) {sele [I] = 1; attempt (); sele [I] = 0 ;}}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.