First, title:
With the development of the Forum, the administrator found that "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 exceeded the sum of 1/4. Please design the algorithm to find them quickly from the list.
Second, design ideas:
The extension topic and the original question are basically similar, the method is the same, all can eliminate the thought. Can be three comparisons, if not the same, then delete, if the same, then the counter plus one.
Third, the source code:
1 //find small "water king"--Huia Bao--2015/04/262 3#include"stdafx.h"4#include"iostream"5 6 7 8 int_tmain (intARGC, _tchar*argv[])9 {Ten intLen; One intidsum[3]={0,0,0}; A //int a[100]; - intid[3]={0,0,0}; -printf"Please enter the sum of the posts:"); thescanf"%d",&len); - int* shuiwang=New int[Len]; -printf"Please enter the ID of the person posting:"); - for(intm=0; m<len;m++) + { -scanf"%d",&shuiwang[m]); + } A at for(intI=0; i<len;i++) - { - /*First initialize the ID list to find three different IDs*/ - if(idsum[0]==0&& shuiwang[i]!=id[1] && shuiwang[i]!=id[2]) - { -idsum[0]=1; inid[0]=Shuiwang[i]; - } to Else if(idsum[1]==0&& shuiwang[i]!=id[0] && shuiwang[i]!=id[2]) + { -idsum[1]=1; theid[1]=Shuiwang[i]; * } $ Else if(idsum[2]==0&& shuiwang[i]!=id[0] && shuiwang[i]!=id[1])Panax Notoginseng { -idsum[2]=1; theid[2]=Shuiwang[i]; + } A /*start to find, different flags idsum+1, same flag -1*/ the Else if(shuiwang[i]!=id[0] && shuiwang[i]!=id[1] && shuiwang[i]!=id[2]) + { -idsum[0]--; $idsum[1]--; $idsum[2]--; - } - Else if(shuiwang[i]==id[0]) the { -idsum[0]++;Wuyi } the Else if(shuiwang[i]==id[1]) - { Wuidsum[1]++; - } About Else if(shuiwang[i]==id[2]) $ { -idsum[2]++; - } - A } + the -printf"%d\t%d\t%d\t\n", id[0],id[1],id[2]); $ the return 0; the}
Iv. Results of Operation:
Five, Experience:
This time the extension problem is similar to the previous problem, the method is the same, a few places can be modified to solve the problem.
Although the method is the same, but the specific method in dealing with this problem is still different, such as counter changes, a person into three people, the three people as a whole and so on. In the process of solving, I have a deeper understanding of the problem, and the previous solution also better understand.
Software Engineering class Assignment (15)--Pulling out "water king" continued