The greedy algorithm is used to solve the sandbags packing problem.

Source: Internet
Author: User

The greedy algorithm is used to solve the sandbags packing problem.

This is a sandbags Packing Problem Baidu knows. My basic idea for solving this problem is to use greedy algorithms, also known as greedy algorithms. The principle of greedy algorithms is to find the best solution.

 

The problem is described as follows:
There are a pile of sandbags, each of which is converted from 1 to 100.
Now we need to load these sandbags into a box with a volume of 100.
The question is, how can I pack these sandbags with the least number of boxes?

My idea is as follows:
If you want to use the least number of boxes, the boxes should be filled as much as possible. To achieve this goal, you need to consider the combination strategy.
A large number of sandbags are difficult to combine with other sandbags. Therefore, you should first put them in the box and then combine them with other sandbags.
So the algorithm should be like this:
First, sort from big to small to get sequence,
Then extract the first element (the largest element) and delete it from sequence.
Then, the next element is taken out, and the element is added to the first element. If it is less than 100, the element is deleted from sequence A and the next element is taken for this step.
If it is greater than 100, skip this element and continue this step until all element traversal is complete.
Record the above sequence and calculate sequence A according to the above steps until sequence A contains no elements.
The Code is as follows:

1 class Program 2 {3 static void Main (string [] args) 4 {5 try 6 {7 int [] sandPackages = new int [] {23, 42, 63, 66, 23, 42, 65, 23, 5, 32, 65, 20}; 8 9 int tankSize = 100; 10 11 List <int> sandLst = new List <int> (); 12 sandLst. addRange (sandPackages); 13 14 // sort 15 sandLst. sort (); 16 // flip. After flip, the internal sorting is from large to small 17 sandLst. reverse (); 18 19 List <int> tankLst = new List <int> (); 20 21 // loop, process the array until all data is 22 while (sandLst. Count! = 0) 23 {24 // find a sequence with a data addition closest to 100 25 tankLst. add (Add2Tank (sandLst, tankSize); 26} 27 28 // display 29 foreach (List <int> sands in tankLst) 30 {31 int temp = 0; 32 foreach (int sand in sands) 33 {34 temp + = sand; 35 Console. write (sand); 36 Console. write (""); 37} 38 Console. write ("total:" + temp); 39 Console. writeLine (); 40} 41 42} 43 catch (Exception ex) 44 {45 throw ex; 46} 47} 48 49 private static List <int> Add2Tank (List <int> sandLst, int tankSize) 50 {51 List <int> sandLst2Tank = new List <int> (); 52 int nowPos = 0; // current position 53 int nowSize = 0; // current total 54 // traverse array 55 while (nowPos <sandLst. count) 56 {57 // Add the value of the current position to the current total 58 if (nowSize + sandLst [nowPos]) <= 100) 59 {60 // if the total current sum after calculation is less than or equal to 100, the data at the current position will be put into the list to be output (that is, packed) 61 // and remove 62 sandLst2Tank from the original array. add (sandLst [nowPos]); 63 nowSize + = sandLst [nowPos]; 64 sandLst. removeAt (nowPos); 65} 66 else67 {68 nowPos ++; 69} 70} 71 72 return sandLst2Tank; 73} 74}

 

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.