Poj1323-Game Prediction (Greedy idea), gameprediction

Source: Internet
Author: User

Poj1323-Game Prediction (Greedy idea), gameprediction

Greedy idea: Try to find the maximum value. Then find the value from the maximum value among the remaining values.
I. Question:
M Individual, N cards per person, each round of comparison of who made the card, the winner. Given M, N, and your cards, we want to output at least several rounds of victories that you can win.
We can see from "at least several rounds of winning" that everyone must start from the biggest card. (Only two cases are supported)
Ii. Ideas:
1. Input and sorted from small to large;
2. Loop and record the number of wins;
3. output;
Step 3:
1. sort by sort function
2. First compare the largest card, then find the largest card comparison from the remaining card, and then loop down until the cards are finished.
Cycle start: I = N-1 (the biggest card in the hand) cycle condition win + lose! = N (cards are not finished) loop processing I -- (the cards in the hands of each loop jump to the previous one );
If the maximum card value in the hand is equal to the maximum card value in the card, win ++; max --;
Else must enter lose ++; max-= 2;
Note: If you win, the biggest card value is reduced by one, and you lose by two.
3. output.

1 # include & lt; iostream & gt; 2 # include & lt; algorithm & gt; 3 using namespace std; 4 int main () {5 int m, n, Case = 0; 6 int a [1005]; 7 while (cin> m> n, m | n) {8 Case ++; 9 for (int I = 0; I <n; I ++) {10 cin> a [I]; 11} 12 sort (a, a + n); // sort from small to large 13 int win = 0, lose = 0; 14 int max = n * m; // The largest card in the storage card 15 for (int I = n-1; win + lose! = N; I --) {// win + lose = n indicates that the card has been played 16 if (max = a [I]) {17 win ++; // The biggest card in the card = the biggest card in the hand, it will win an 18 max --; // It will win, the maximum card value in the card can be reduced by 19} 20 else {// otherwise, it will be lost once. 21 lose ++; // record the number of inputs 22 max-= 2; // lost, the maximum card value in the card is reduced by two 23} 24} 25 cout <"Case" <Case <":" <win <endl; 26} 27 return 0; 28}View Code

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.