Find the Water King 2

Source: Internet
Author: User

    • With the development of the Forum, the administrator found that the water king did not, but the statistical results show that there are three posts a lot of ID. According to the statistics of their posts more than 1/4, you can quickly find them from the list of posts?

Design ideas:

Here are the design ideas for Water King 1:

This is the problem of finding the highest frequency in an array, not the same as a point in a known condition where the maximum frequency of occurrence is greater than 0.5.

You can count the number of occurrences of each ID in a regular way. This assignment requires a new idea, i.e.

1. The first shuiwangid=id[0] has appeared times=1 times;

2. Traverse the next ID, if same as Shuiwangid, times++, otherwise, times--。 If ntimes = = 0, repeat the first step;

3. The end of the traversal, Shuiwangid is the water King ID.

The difference is that the number of water kings has become 3. The above algorithm can still be continued, but it needs to be modified, since three water kings do not conflict with each other, it is possible to define three water kings, and then offset with other IDs.

Specific ideas are as follows:

1. Three water Kings id:shuiwang1, Shuiwang2, Shuiwang3 have appeared times1=times2=times3=0 times respectively;

2. Traverse the next ID and compare with three water King ID, if the same as SHUIWANGN, timesn++, otherwise, Times1, Times2, Times3 all minus one.

3. If the number of occurrences of a SHUIWANGN timesn==0 and the ID of the current traversal is different, set the ID of the current traversal to this SHUIWANGN ID and make timesn=1. Continue to the second step until the end of the traversal.

3. The shuiwang1, Shuiwang2, and Shuiwang3 are the IDs of the three water kings respectively at the end of the traversal.

Code:

1#include <iostream>2#include <fstream>3#include <string>4 using namespacestd;5 intMain ()6 {7Ifstream Idfile ("ID.txt");8     stringbuffer;9     stringShuiwang1 ="";Ten     stringShuiwang2 =""; One     stringShuiwang3 =""; A     intIdnum =0;; -     intTimes1 =0; -     intTimes2 =0; the     intTimes3 =0; -cout <<"ID List:"; -      for (;;) -     { +         if(idfile.eof ()) -         { +              Break; A         } atIdfile >>buffer; -Idnum + =1; -cout << Endl <<buffer; -         if(Shuiwang1 = =buffer) -         { -Times1 + =1; in         } -         Else if(Shuiwang2 = =buffer) to         { +Times2 + =1; -         } the         Else if(Shuiwang3 = =buffer) *         { $Times3 + =1;Panax Notoginseng         } -         Else if(Times1 = =0) the         { +Times1 + =1; AShuiwang1 =buffer; the         } +         Else if(Times2 = =0) -         { $Times2 + =1; $Shuiwang2 =buffer; -         } -         Else if(Times3 = =0) the         { -Times3 + =1;WuyiShuiwang3 =buffer; the         } -         Else Wu         { -Times1-=1; AboutTimes2-=1; $Times3-=1; -         } -     } - idfile.close (); Acout << Endl <<"Total"<< Idnum <<"stickers. "<< Endl << Endl <<"Water King 1ID:"<< shuiwang1 << Endl <<"Water King 2ID:"<< shuiwang2 << Endl <<"Water King 3ID:"<< Shuiwang3 <<Endl; +     return 0; the}
View Code

Personal Summary:

Although

Find the Water King 2

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.