Classroom practice-Finding water king

Source: Internet
Author: User

A three-person line designed a forum for irrigation. Information college students like to exchange irrigation above, legend in the forum there is a "water king", he not only likes to post, but also reply to other ID issued by each post. The "Water King" has been rumored to have posted more than half the number of posts.

Request: If you have a list of posts (including replies) for the current forum, and the ID of the author of the Post is in it, can you quickly find the legendary water king? Solution: to the problem of simplification, because "water king" post number of more than half of the number of posts, which can be used as a breakthrough, to find a simple way to solve, you can post the ID as an array, simplified to, in an array, find more than half the same elements. The elimination method is relatively simple, that is, the different elements of the adjacent elimination, the same element, then not eliminate, and num (count) plus 1, the final remaining element is the element that asks for the search. Code:
//Find the Water king//Euna Lee 20133093//a three-person line designed a forum for irrigation. The school of information students like to exchange irrigation above, legend in the forum there is a "water king",//not only does he like to post, but it also responds to every post that other IDs send. The "Water King" has been rumored to have posted more than half the number of posts. //Request: If you have a list of posts (including replies) for the current forum, and the ID of the author of the Post is in it, can you quickly find the legendary water king? #include <iostream>using namespacestd;#defineN 100intMain () {intWater_king =0;//to mark the water king .    intnum =0;//number of times a record has been eliminated    intNum_tiezi =0;//record the number of posts    intTie[n];  while(Num_tiezi = =0)//if the array length is empty or zero, re-enter{cout<<"Please enter the number of posts:"; cout<<Endl; CIN>>Num_tiezi; } cout<<"Enter the Post ID:"<<Endl;  for(inti =0; I <= (Num_tiezi-1); i++) {cout<<"("<<i+1<<"):"; CIN>>Tie[i]; cout<<Endl; if(Tie[i] = =0) {cout<<"The ID cannot be 0, please re-enter:"; cout<<"("<< i +1<<"):"; CIN>>Tie[i]; cout<<Endl; }    }     for(inti =0; i<num_tiezi; i++)    {        if(num = =0)//initially, the first element of the array is assigned to water_king, if NUM is 0, representing the former I-1 element 22 is eliminated, re-assigned the value{water_king=Tie[i]; Num=1; }        Else        {            if(water_king = = Tie[i])//equality does not eliminatenum++; ElseNum--; }} cout<<"the ID of the Navy is:"<< water_king <<Endl; return 0;}

Test results:

Experiment Summary: This experiment solves the method to have many kinds, takes the time complexity, chooses this method, can reduce the time complexity to the minimum, simultaneously the code realizes, is also relatively simple. At the same time, this method can be used to solve the problem later, providing another different way of thinking.

Time log:

Classroom practice-Finding water king

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.