First, 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 statistics their number of posts more than 1/4, you can quickly find them from the list of posts
Second, design ideas:
Consistent with the original idea, using a mutually offsetting approach. Last time 1 people more than half, now is 4 people more than 1/4, so for (n-1) people more than 1/n can be counted. The idea is this, each time to remove the n different, when the remaining ID is not the same as N, the rest is to find the ID.
Third, the program code:
1#include"iostream" 2 using namespacestd;3 #defineMAXSIZE 304 5 voidFindwater (intId[],intLenintwater[])6 { 7 intcounter[3];//counter 8counter[0]=counter[1]=counter[2]=0; 9water[0]=water[1]=water[2]=-1;//Initialize list of suspectsTen for(intI=0; i<len;i++) One { A if(counter[0]==0)//Assigning values First - { -counter[0]++; thewater[0]=Id[i]; - } - Else if(counter[1]==0) - { +counter[1]++; -water[1]=Id[i]; + } A Else if(counter[2]==0) at { -counter[2]++; -water[2]=Id[i]; - } - Else if(id[i]==water[0])//If counter is 0, re-assign the value - { incounter[0]++; - } to Else if(id[i]==water[1]) + { -counter[1]++; the } * Else if(id[i]==water[2]) $ {Panax Notoginsengcounter[2]++; - } the Else //if the IDs of the three suspects are different, the three counters are reduced by one until the counter is 0 . + { Acounter[0]--; thecounter[1]--; +counter[2]--; - } $ } $ } - - intMainintargcChar*argv[]) the { - intId[maxsize];//Post record formWuyi intnum,waterking[3];//number of posts, buckets thecout<<"Please enter the total number of posts:"; -Cin>>num; Wucout<<"Please enter all the IDs for the post:"; - for(intI=0; i<num;i++) About { $Cin>>Id[i]; - } -Findwater (id,num,waterking); -cout<<"buckets are:"; A for(intI=0;i<3; i++) + { thecout<<waterking[i]<<" "; - } $cout<<Endl; the return 0; the}
Four, the operation
v. Project plan log
Dates && Tasks |
Lectures |
Writing Programs |
Read related books |
Find information online |
Total Day |
Monday |
100 |
|
30 |
30 |
160 |
Tuesday |
|
|
30 |
30 |
60 |
Wednesday |
|
30 |
30 |
10 |
70 |
Thursday |
100 |
20 |
30 |
|
150 |
Friday |
|
120 |
30 |
30 |
180 |
Saturday |
|
|
|
|
|
Sunday |
|
|
|
|
|
Weekly totals |
200 |
170 |
150 |
100 |
620 |
Time Recording Log
5/23
Date |
Start time |
End time |
Interrupt Time |
NET time |
Activities |
Note |
5/23 |
14:00 |
15:50 |
10 |
100 |
Lectures |
Software Engineering Classes |
|
21:04 |
21:34 |
0 |
30 |
Read books |
"Dream Break Code" |
|
22:10 |
22:40 |
0 |
30 |
Find information online |
|
5/24 |
18:00 |
18:30 |
0 |
30 |
Read books |
"Dream Break Code" |
|
22:15 |
22:45 |
0 |
30 |
Find information online |
|
5/25 |
19:25 |
20:00 |
5 |
30 |
Writing Programs |
Find the Water King 2 |
|
22:00 |
22:30 |
0 |
30 |
Read books |
"Dream Break Code" |
|
22:40 |
22:50 |
0 |
10 |
Find information |
|
5/26 |
14:00 |
15:50 |
10 |
100 |
Class |
Software Engineering on-machine |
|
18:26 |
18:50 |
4 |
20 |
Writing Programs |
Find the Water King 2 |
|
22:00 |
22:30 |
0 |
30 |
Read books |
"Dream Break Code" |
5/27 |
14:00 |
16:20 |
20 |
120 |
Writing Programs |
Find the Water King 2 |
|
11:23 |
12:00 |
7 |
30 |
Find information online |
|
|
21:00 |
21:30 |
0 |
30 |
Read books |
The law of Construction |
Vi. Personal Summary
For this topic, the beginning of the train of thought, I think is also the majority of students have some ideas, is the sort, the big deal on a one to find and then accumulate the total number of their ID number, but this method is not easy in terms of complexity and many aspects, and then in the teacher's prompt "subtraction", And then there's a new thought. Later the teacher mentioned the traversal and then sort, the middle is definitely the water King ID, but the time complexity for n*n, in order to reduce the complexity of time, the teacher suggested with 22 elimination of ideas, then we thought how to solve. Thus, our thinking is not very open, confined to those who have learned the simple common knowledge, so I still have a lot of room for progress. Extrapolate solves the problem of multiple buckets by expanding the title of the water-seeking king. You should practise more in class to make practice perfect.
Find the Water King 2