First, the problem description
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?
Second, the idea of settlement
According to the last Water king method, I tried this time is "consumer music" method, but unlike the last time, in this code, the counter has become three,
At the same time, by a "water king", turned into three "buckets", thinking and the same as the last time.
Third, the source code
//Tian Yongtao//2015.4.25#include"stdafx.h"#include<stdio.h>#defineLength 21//set maximum length tointMIN (intNum[])//find the smallest number in count[] to reduce by 1{ intmin; Min= num[1]; for(intI=1;i<3; i++) { if(min>num[i]) min=Num[i]; } returnmin;}voidJudgeintArr[length]) { intI,j =0; intFLAG1,FLAG2,FLAG3;//used to represent buckets . intcount[3] = {0,0,0};//to account for the current number of water Kings .Flag1 =-1;//Initializing BucketsFlag2 =-1; Flag3= -1; while((flag1==-1)|| (flag2==-1)|| (flag3==-1))//The while loop does the first three of the inequality in the ID table and serves as a bucket { if((Flag1 = = Arr[j]) | | (Flag1 = =-1) ) {Flag1=Arr[j]; count[0]++; } Else if((Flag2 = = Arr[j]) | | (Flag2 = =-1))//assign a bucket to a number in the ID table{Flag2=Arr[j]; count[1]++; } Else if((Flag3 = = Arr[j]) | | (Flag3 = =-1) ) {Flag3=Arr[j]; count[2]++; } J++;//used to count, to record the number of IDs that have been traversed in the current ID table } for(i=j;i<length;i++)//use the "Eliminate music" method to identify the bucket in the ID table { if(Flag1 = = Arr[i])//When the current ID is equal to a known bucket{Flag1=Arr[i]; count[0]++; } Else if(Flag2 = =Arr[i]) {Flag2=Arr[i]; count[1]++; } Else if(Flag3 = =Arr[i]) {Flag3=Arr[i]; count[2]++; } Else //d when the current ID is not equal to the three ID of a known bucket { if(count[0] = = MIN (count))//find the minimum count, minus 1{count[0]--; if(count[0]<1) {Flag1= arr[i+1]; count[0] =1; I++; } } Else if(count[1] ==MIN (count)) {count[1]--; if(count[1]<1) {Flag2= arr[i+1]; count[1] =1; I++; } } Else if(count[2] ==MIN (count)) {count[2]--; if(count[2]<1) {Flag3= arr[i+1]; count[2] =1; I++; } }}} printf ("The bucket ID is:%d%d%d\n", FLAG1,FLAG2,FLAG3);//Output Results}intMainintargcChar*argv[]) { intArray[length] = {1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3,4,4,4}; printf ("the contents of the ID table are: \ n"); for(intI=0; i<length;i++) {printf ("%d", Array[i]); } printf ("\ n"); Judge (Array); return 0;}
Four, the operation
V. Summary
This class work is almost the same as the last time, at least the implementation of the code in the mind is the same, but I think in some small details are still different.
For example: When assigning a value to a bucket flag1,flag2,flag3, use Flag1 = Arr[0],flag2 = arr[1],... When it's obviously inappropriate because the first few IDs
Probably the same, so, in the process of code implementation, I used the while statement (the implementation method to see the code), the ID table in the first different three assignments, and
For counter Count[i] + +,
This will solve the problem.
Classroom Practice _ Buckets