I. Description of the topic
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 solution of ideas
Follow the idea of the last elimination, define three counters and three suspected water King, compare three ID, if not the same, exclude, if the same, then the counter plus one. The ID of the suspected water King is offset with the other Navy IDs. The last thing left is three water kings.
Third, the program code
//shuiba.cpp: Defines the entry point of the console application. //#include"stdafx.h"#include"iostream"intMain () {intlength; intidcou[3]={0,0,0};//counter initialized to 0 intid[3]={0,0,0};//Water King ID initialized to 0printf"Please enter the number of posts:"); scanf ("%d",&length); int* shuiwang=New int[length]; printf ("Please enter the ID of all the people posting:"); for(intj=0; j<length;j++)//enter ID{scanf ("%d",&Shuiwang[j]); } for(intI=0; i<length;i++)//Find the Water king { if(idcou[0]==0&& shuiwang[i]!=id[1] && shuiwang[i]!=id[2]) {idcou[0]=1; id[0]=Shuiwang[i]; } Else if(idcou[1]==0&&shuiwang[i]!=id[0] && shuiwang[i]!=id[2]) {idcou[1]=1; id[1]=Shuiwang[i]; } Else if(idcou[2]==0&&shuiwang[i]!=id[0] && shuiwang[i]!=id[1]) {idcou[2]=1; id[2]=Shuiwang[i]; } Else if(shuiwang[i]!=id[0] && shuiwang[i]!=id[1] && shuiwang[i]!=id[2]) {idcou[0]--; idcou[1]--; idcou[2]--; } Else if(shuiwang[i]==id[0]) {idcou[0]++; } Else if(shuiwang[i]==id[1]) {idcou[1]++; } Else if(shuiwang[i]==id[2]) {idcou[2]++; }} printf ("the ID of three water Kings is: \ n"); printf ("%d\t%d\t%d\t\n", id[0],id[1],id[2]); return 0;}
Iv. Results of operation
Five, experience
There are a lot of similarities between the three water kings and a water king, but the problem is wrong at first, and then the cause of the problem is found and some of the possibilities are ignored. After two exercises to find the water king, I think the most bull is not to find the best way to solve the problem, but the person who is behind the scenes.
Software Engineering Classroom Training ——— find three of the Kings