First, the topic
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 statistics their number of posts more than 1/4, you can quickly find them from the list of posts
Second, design ideas
The solution to the problem is that more than half of the water king, now become more than 1/4, then the rest will not be more than 1/4, which can be analyzed, if each comparison, to remove the different three of the data, then, with the removal of more than 1/2 of the number of IDs found in the solution is the same, so, We can set an array to store three different IDs, and then find the three matching IDs from the Waterking list, and if so, then the Count flag single[]+1, if it doesn't match, all the ID's flag bit-1, then the last remaining is definitely the number of three IDs
Third, the source code (on the original basis to improve)
1 //waterking.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include"iostream"6 using namespacestd;7 8 voidData (intLengthinta[])9 {Tencout<<"Please enter a list of eligible IDs (where the number of three IDs is more than 1/4, and the other IDs are less than three):"<<Endl; One for(intI=0; i<length;i++) A { -Cin>>A[i]; - } the } - - intMain () - { + intlength; - intsingle[3]={0,0,0}; + intid[3]={-1,-1,-1};//set the semaphore Acout<<"Please enter the number of posts:"; atCin>>length; - int* waterking=New int[length]; - Data (length,waterking); - for(intI=0; i<length;i++) - { - /*First initialize the ID list to find three different IDs*/ in if(single[0]==0&& waterking[i]!=id[1] && waterking[i]!=id[2]) - { tosingle[0]=1; +id[0]=Waterking[i]; - } the Else if(single[1]==0&& waterking[i]!=id[0] && waterking[i]!=id[2]) * { $single[1]=1;Panax Notoginsengid[1]=Waterking[i]; - } the Else if(single[2]==0&& waterking[i]!=id[0] && waterking[i]!=id[1]) + { Asingle[2]=1; theid[2]=Waterking[i]; + } - /*start to find, different flags single+1, same flag -1*/ $ Else if(waterking[i]!=id[0] && waterking[i]!=id[1] && waterking[i]!=id[2]) $ { -single[0]--; -single[1]--; thesingle[2]--; - }Wuyi Else if(waterking[i]==id[0]) the { -single[0]++; Wu } - Else if(waterking[i]==id[1]) About { $single[1]++; - } - Else if(waterking[i]==id[2]) - { Asingle[2]++; + } the - } $cout<<"Water King Absalom:"<<id[0]<<id[1]<<id[2]<<Endl; the return 0; the}
Iv. Results of the experiment
V. Analysis of experimental results
Many problems have common ground, if we can find a few problems in common, then, if you could solve a problem, you can solve a number of problems, you do not have to re-examine the problem of analysis, just need to expand on the original basis to be able to, like this time to find water king, Find A and find three actually no difference, a person posts more than half, 3 people post more than 1/4, then 3 people add up also more than half, if three people as a whole, then with a person's time becomes the same problem.
Therefore, in solving the problem, we may as well first do the analysis, to solve the problem is similar to a problem or the same principle, and how to solve the problem, to solve the problem should be how to transform, it can be the same as this problem. If we can correctly analyze the problem, the final result will certainly be more effective, save a lot of time
Find Water King Extension