Find the Water King (ii)

Source: Internet
Author: User

Topic:

A 13-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.

2 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? (Refer to Core code)

3 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

The trial has three water kings are more than 1/4 of the total number of posts, can be each water King ID and other ID to offset the last remaining is three water king, because the number of user ID is not visible, do not know which ID is water king, you can put the first three different ID for the water King ID, They are offset by an ID that is different from the current three IDs, and the remaining 0 of the "Water King" is eliminated, adding the current different ID to the water king to continue to cancel, as the four water kings cancel together, while the three true water kings exceed the 1 of all water-paste IDs 4, so the last remaining one must be three water King ID

Source:

#include <iostream> #include <assert.h>using namespace Std;int * Search (int *userid,int n) {int curid[3]={0 };//the number of three different idint currently saved curnum[3]={0};//the current three different IDs; for (int i=0;i<n;i++) {if (curnum[0]==0&&userid[i]!= CURID[1]&AMP;&AMP;USERID[I]!=CURID[2])//user ID does not have the same ID in the current ID and the current number of userid[0] is 0; {curnum[0]=1;curid[0]=userid[i];} else if (curnum[1]==0&&userid[i]!=curid[0]&&userid[i]!=curid[2]) {curnum[1]=1;curid[1]=userid[i];} else if (curnum[2]==0&&userid[i]!=curid[0]&&userid[i]!=curid[1]) {curnum[2]=1;curid[2]=userid[i];} else if (userid[i]==curid[0])//user ID and current ID are the same as the current ID number +1{curnum[0]++;} else if (userid[i]==curid[1]) {curnum[1]++;} else if (userid[i]==curid[2]) {curnum[2]++;} else if (userid[i]!=curid[0]&&userid[i]!=curid[1]&&userid[i]!=curid[2])//Four IDs together offset {curNum[0]--; curnum[1]--;curnum[2]--;}} Return curid;//returns the current remaining three IDs as three water Kings}void main () {int N;int * userid;int curid[3]={0};int curnum[3]={0};int i;cout<< " Input number of posts: "; Cin>>n;assert (n>0); userId=new int[n];cout<< "Enter each drip post ID:" <<endl;for (i=0;i<n;i++) {cin>>userid[i];} for (i=0;i<3;i++)//Output three water King id{cout<<* (Search (userid,n) +i) << "\ T";}}

Experimental summary

On the basis of the previous need to broaden the thinking, four consecutive user ID cancellation is difficult to achieve, the time complexity will be very large, and then the actual operation only to find that the same ID is accumulated, different from the current three ID four to cancel together,,

Find the Water King (ii)

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.