Q5_packing problem focus, seriously, think, stick to, don't give up

Source: Internet
Author: User

The serial number for this question is 1017.

After I submitted my comments to my senior friend in Q4 last time, I pasted them here to remind myself in the future:

"

I found that there are several problems when you do the question. there are too many answers to the standard, and you should try your best to come up with them. These questions are common questions, but you still have less exercise in computer thinking, this kind of thinking is necessary for subsequent project programming. 2. If you cannot answer the question, you can look at the standard answer, but not the standard procedure. You can only look at the "problem-solving ideas", but cannot definitely look at the "reference program ". I learned how to solve the problem from the problem-solving thinking, but "how to implement" is also a capability that requires exercise. "Reference Program" is never allowed to be viewed before you have "accept! 3. Write a program and look at books similar to computer algorithms and data structures, which is good for improving programming thinking. I don't have to worry about reading these books, but it is enough to read them.
I used to work as hard as you did, but I found that I did not learn well. Later I learned the reason: "Physical diligence is not really hard work, but hard work in my mind. I am physically diligent, but don't think about it. I only know that it is a kind of laziness"

"

Today, this question has been done since the morning, but it is very rewarding:

First, let me know that any problem will always be solved if I think hard.

At the beginning, I thought it was very simple. I quickly wrote a version and wrote a runtime error after submission. I guess the array I defined is too large, so I changed the input to dynamic memory allocation, this time there was no error, but wrong answer was gone, and it was not that easy to think about it, so I analyzed it on paper and thought about the last time I submitted Q4 to my brother, the senior engineer said that these are all regular questions. He should try not to look at the ideas and answers to the questions. I thought I could not see the reference. Since it was a regular question, I was sure I had come up with a solution. I used a pen to draw on paper. I quickly implemented it using a program, wrong answer is submitted several times. During the submission process, I always saw an accepted, so I was a little worried. I insisted on my own program and didn't find any errors. Later, ask the senior brother, said

In this case, first look at the problem-solving ideas in the document to see if there are any problems with the ideas. If there is no problem with the idea and a bug is reported several times, you may ask someone to help you look at the code, or you may look at the reference program. In fact, the reference program is after you fully think about it, write code repeatedly, debug and submit it, it is best to compare the effect of the Comparison Program, look for problems, and see which one is not considered clearly. The answer to these questions is not important. What is important is the process of thinking about the problem and solving the problem using a program. In fact, each question can be regarded as a small project and is a process of exercise. Each time you finish a question based on your own abilities, it is similar to completing a project or arranging a task with your own little effort. The process of solving these questions is similar to the process of completing tasks in the company. Therefore, if you do a lot of questions, you will be dominant during the interview. Even in the Unit, the task can be well arranged. If you stick to the habit of graduation, you will be far away from Daniel.

After listening to what my senior friend said, I read the reference ideas and analysis. After reading a few words, I was very happy because it was consistent with my own analysis, this is not what I said a few days ago. When can I think of this method all at once? This time, though not all at once, I also came up with my own ideas. I would like to praise myself for your encouragement ~, That's right. Where is the error? Let's look at it again .. I was so careless that I had missed out on the remaining 1*1 space. The study should be rigorous and coding should be careful. Once you know where your mistakes are, you will be confident that you will soon make up for the missing information and submit the accepted.

There are still some differences between my code and the code provided by the reference. paste them here, compare them, and select the optimal one. In future programming, notice:

My:

# Include <stdio. h> # include <stdlib. h> int main () {int order [6]; int I, j, total; // dynamically allocates memory int * P; P = (int *) malloc (sizeof (INT); // enter the number of orders for (j = 0; j ++) {scanf ("% d", & Order [0], & Order [1], & Order [2], & Order [3], & Order [4], & Order [5]); Total = 0; total = total + order [5] + order [4] + order [3]; // process a box of 4 *, 5*5, 6*6 int S1 = 0, s2 = 0; // S1 stores the remaining 1*1 space, S2 stores the remaining 2*2 space, S2 = 5 * Order [3]; s1 = 11 * Order [4]; // process 3*3 boxes. First, you need 3*3 boxes, do not forget the 3*3 remaining 1*1 If (Order [2] % 4 = 0) {Total = total + order [2]/4 ;} else if (Order [2] % 4 = 1) {Total = total + order [2]/4 + 1; S2 = S2 + 5; S1 = S1 + 7 ;} else if (Order [2] % 4 = 2) {Total = total + order [2]/4 + 1; S2 = S2 + 3; S1 = S1 + 6 ;} else if (Order [2] % 4 = 3) {Total = total + order [2]/4 + 1; S2 = S2 + 1; S1 = S1 + 5 ;} // handle 2*2 boxes, pay attention to the remaining space after each step, int K = 0; k = s2-order [1]; If (k> = 0) {S1 = S1 + K * 4;} else {If (-k) % 9 = 0) {Total = total + (-k)/9 ;} else {Total = total + (-k)/9 + 1; S1 = S1 + (9-(-k) % 9) * 4 ;}} // handle 1*1 box, int T = 0; t = s1-order [0]; If (T <0) {If (-T) % 36 = 0) {Total = total + (-T)/36;} else {Total = total + (-T)/36 + 1 ;}} if (Total = 0) {break;} else {P [J] = total; // store each result in the dynamic array P [J].} for (I = 0; I <J; I ++) {printf ("% d \ n", P [I]);} return 0 ;}

Reference:

# Include <stdio. h> int main () {int N, A, B, C, D, E, F, Y, X; // n is used to store the number of required boxes, Y is used to store 2*2 vacancies // X is used to store 1*1 vacancies. Int U [4] = {0, 5, 3, 1}; // array U indicates that the number of products 3*3 is a multiple of 4, a multiple of 4 + 1, when the multiples of 4 are + 2 and 4 are + 3 //, the number of 2*2 vacancies remaining in the new box opened by 3*3 products while (1) {scanf ("% d", & A, & B, & C, & D, & E, & F ); if (A = 0 & B = 0 & C = 0 & D = 0 & E = 0 & F = 0) break; N = F + E + D + (C + 3)/4; // here is a tip-(C + 3) /4 is equal to the result of dividing C by 4 and rounded up, the same below y = 5 * D + U [C % 4]; If (B> Y) N + = (B-y + 8)/9; X = 36 * n-36 * F-25 * E-16 * D-9 * C-4 * B; if (A> X) N + = (a-x + 35)/36; printf ("% d \ n", n);} return 0 ;}

The reference program provided below looks simple and the most important thing is simple here:

Take a long string of my judgments

If (-T) % 36 = 0)

{Total = total + (-T)/36;

} Else {

Total = total + (-T)/36 + 1;

}

Use a sentence (-t) + 35)/36 to implement the (-T)/36 rounded up result;

It is also a clever method to place the differences between the remainder of 3*3 and the remaining 2*2 space in an array:

Int U [4] = {0, 5, 3, 1 };

However, there is a problem where the multi-row input does not meet the multi-row output conditions.

Two images are attached. One is to record the frustration chart of today's time:

Figure 2 shows a draft of ideas on paper:

 

 

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.