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