[Program Design Basics _ c Language] the evil dragon and Program Design Basics of Beijing Institute of Technology _ c

Source: Internet
Author: User

[Program Design Basics _ c Language] the evil dragon and Program Design Basics of Beijing Institute of Technology _ c
Evil dragon of Beijing Institute of Technology (attached qsort instance)

Background:
Recently, Beijing Institute of Science and Technology has a dragon with many heads and fires. It will burn Beijing Institute of Science and Technology into ruins. As a result, the principal ordered all the warriors to kill the dragon. To kill this dragon, you must cut down all its headers. Each warrior can only cut one faucet. Each of the dragons has a different size. A warrior is only in height.Not lessThe diameter of the faucet can be cut down. The warriors demanded that the next leader be given the same credits as the height in centimeters. The principal wants to take the least score to kill the evil dragon, so he finds you for help.

Input:
The number of leaders in the first line is n, and the number of warriors is m (1 <= n, m <= 100). The next n rows contain an integer, indicating the diameter of the faucet. Next m rows, each line contains an integer indicating the height of the warrior l

Output:
If the warriors can complete the task, output the minimum cost required by the principal; otherwise, enter "bit is doomed! "


  Test input expected output time limit memory limit additional processes
Test Case 1 Display in text Display in text 1 second 64 M 0
Test Case 2 Display in text Display in text 1 second 64 M 0

[Analysis] The problem is abstracted into two Arrays: dragon and soldier, respectively storing the height of the warrior and the diameter of the faucet. If a value in the array soldier is greater than or equal to a value of dragon, the leader is successfully chopped and the height of the warrior is counted into sum. Requires the minimum "credit consumption", that is, priority is given to the warriors with relatively low height to the current minimum leader, so the first two arrays are sorted from small to large. When there is a leader left (I <= n) and no warriors can cut the leader (j> m), it fails. Yes Note:If the current warriors have successfully been killed, they must move to the next leader and Next warriorMobile (because a warrior can only lead one node ).
[Code]
// E026: [] the evil dragon # include "stdio. h "# include" stdlib. h "int cmp (const void * a, const void * B) // The sorting function {return * (int *) a-* (int *) B ;} int cmp (const void * a, const void * B); int main () {int n; // n is the number of leaders int dragon [102] = {0 }; // dragon is the diameter of the faucet int m; // m is the number of warriors int soldier [102] = {0}; // solider is the height of the Warrior int sum = 0; // sum is the total number of credits spent (sum of the length of the beilong operation) int I, j; scanf ("% d", & n, & m ); for (I = 1; I <= n; I ++) // enter the diameter of the faucet {scanf ("% d", & drago N [I]) ;}for (I = 1; I <= m; I ++) // enter the warrior height {scanf ("% d ", & soldier [I]);} qsort (dragon + 1, n, sizeof (int), cmp); // call the function to sort qsort (soldier + 1, m, sizeof (int), cmp); // call the function to sort by soldier/* for (I = 1; I <= n; I ++) // check whether the sorting is successful -- successful {printf ("dragon [% d] = % d \ n", I, dragon [I]);} */for (I = 1, j = 1; I <= n; I ++) {// printf ("I = % d \ n", I ); // monitoring I value if (j> m) {// printf ("Failure: I = % d, j = % d \ n", I, j ); // monitor the failure status goto fail Ure;} if (soldier [j]> = dragon [I]) // if the current warrior is able to kill the leader {sum + = soldier [j]; j ++; // if the result is successful, the warrior is counted into sum, And the next warrior and the next leader are compared. // printf ("warrior I = % d Dalong j = % d success! \ N ", I, j); // indicates that the leader has succeeded in continue;} else // if the current warrior cannot beam the dragon, then the cycle continues until the leader has succeeded, or the failure {while (soldier [j] <dragon [I]) // if the current warrior cannot kill the dragon, the warrior is not counted in sum and compares the next warrior {j ++; if (j> m) // if no other warrior is available, goto failure; else if (soldier [j]> = dragon [I]) fails. // compare the next warrior {sum + = soldier [j]; j ++; break; // if the result is successful, the warrior is counted into sum, and compare the next warrior and the next leader} else {continue; // If the warrior fails, compare the next warrior }}} printf ("% d \ n", sum ); return 0; failure: printf ("bit is doomed! \ N "); return 0;} // main

Qsort: Fast sorting]
# Include "stdlib. h "// qsort requires this header file int cmp (const void * a, const void * B); // function declaration int cmp (const void * a, const void * B) // function definition {return * (int *) a-* (int *) B; // if it is from small to large, replace a and B in this row.} int main () {int dragon [102]; qsort (dragon + 1, n, sizeof (int), cmp ); // call the function to sort the dragon array from dragon [1] and overwrite the parameters of the previous row in the original array: 1. First address of the array to be sorted 2. Number of elements to be sorted 3 space occupied by each element 4. pointer to function return 0 ;}



[Perception] it seems that it is not that difficult, but it is only possible to wrap itself in when I am reading the question.
When I tested it locally, I found that, under some circumstances, after the warrior leader was successful, the warrior forgot to move to the next place.
So it took several minutes to debug it, and the trace was still there.
You are welcome to correct possible errors or propose better algorithms ~~~


Above.

 

Related Article

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.