One, 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?
Second, the idea: (1) All the ID number from small to large to sort, and then count the number of each ID number, to find out the number of occurrences more than 1/4 of the ID number, that is, for the request, but the complexity of the time is too complex.
(2) on the basis of a sort of thinking, the ID number of three water blaster is at 1/4,1/2,3/4 Place respectively.
(3) on the basis of the last class to find the water king, the search for the water bully is to eliminate the continuous 4 different IDs.
Third, the procedure:
#include <iostream.h>
#include <conio.h>
void IntID (int length,int arr[])
{
cout<< "Enter the ID of the person who posted the poster:" <<endl;
for (int i=0;i<length;i++)
{
cin>>arr[i];
}
}
int main ()
{
int length;//number of post ID
int t[3]={0,0,0};
int id[3]={-1,-1,-1};
cout<< "Please enter the number of posts:";
cin>>length;
int * Shuiba=new int [length];
int Jixu;
IntID (Length,shuiba);
for (int i=0;i<length;i++)
{
if (t[0]==0 && shuiba[i]!=id[1] && shuiba[i]!=id[2])
{
T[0]=1;
Id[0]=shuiba[i];
}
else if (t[1]==0 && shuiba[i]!=id[0] && shuiba[i]!=id[2])
{
T[1]=1;
Id[1]=shuiba[i];
}
else if (t[2]==0 && shuiba[i]!=id[0] && shuiba[i]!=id[1])
{
T[2]=1;
Id[2]=shuiba[i];
}
else if (shuiba[i]!=id[0] && shuiba[i]!=id[1] && shuiba[i]!=id[2])
{
t[0]--;
t[1]--;
t[2]--;
}
else if (shuiba[i]==id[0])
{
t[0]++;
}
else if (shuiba[i]==id[1])
{
t[1]++;
}
else if (shuiba[i]==id[2])
{
t[2]++;
}
}
cout<< "Water Blaster ID:" <<ID[0]<< "" <<ID[1]<< "" <<ID[2]<<endl;
cout<<endl;
cout<< "Do you want to continue testing?" (0, Exit 1, continue) "<<endl;
cin>>jixu;
if (jixu==1)
{
cout<<endl;
Main ();
}
Else
{
return 0;
}
Getch ();
return 0;
}
Four, the result of operation:
Five, Summary:
The problem is relatively simple, with the basis of previous classroom exercises to solve similar problems. But the problem is not simply to ask for implementation, but to use the algorithm to optimize and reduce the program run time, which requires us to take the time to consider a better solution. Finding the right solution is the key to solving this kind of problem.
Classroom practice-looking for a water bully