Classroom Practice--Find the Water king continued

Source: Internet
Author: User

Title: 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?

First, design ideas

If you follow the first sort, the three small Wang Wang are at 1/4, 1/2, 3/4, the time complexity is O (NLOGN), do not meet the complexity of time requirements. Refer to the last Water King topic, every time to eliminate two different ID posts, then the last remaining is the water king. This topic can be referred to this method, but once to eliminate four different IDs of posts, to the end, it is still three of their total accounted for more than 1/4. This approach, while satisfying the complexity of time, has slightly improved space complexity.

Second, the source code

#include <iostream.h>intMain () {intid[Ten]={1,2,3,4,1,2,3,1,2,3}; intId_null;//define an ID that does not exist    intshui[3]; intflag[3]; inti; shui[0]=shui[1]=shui[2]=0; flag[0]=flag[1]=flag[2]=Id_null;  for(i=0;i<Ten; i++)    {        if(id[i]==flag[0]) {shui[0]++; }        Else if(id[i]==flag[1]) {shui[1]++; }        Else if(id[i]==flag[2]) {shui[2]++; }        Else if(shui[0]==0) {shui[0]=1; flag[0]=Id[i]; }        Else if(shui[1]==0) {shui[1]=1; flag[1]=Id[i]; }        Else if(shui[2]==0) {shui[2]=1; flag[2]=Id[i]; }        Else{shui[0]--; shui[1]--; shui[2]--; }} cout<<"the Aqua King is:"<<Endl; cout<<flag[0]<<"  "<<flag[1]<<"  "<<flag[2]<<Endl; return 0;}

Iii. Results of operation

Third, personal summary

Rather than simply asking for implementation, the problem requires that we use algorithms to optimize and reduce program uptime, which requires programmers to spend a lot of brainpower. Finding the right solution is the key to solving this kind of problem.

Classroom Practice--Find the Water king continued

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.