First, the topic requirements
There is a forum for irrigation, information college students like to exchange irrigation above. Legend in the forum there is a "water king", it not only likes to post, but also reply to other IDs issued by each post. The number of "Water kings" has been rumored to have exceeded half the number of posts.
If you have a list of posts (including replies) for the current forum, where the author ID of the post is located, design the algorithm to quickly find the legendary "Water King".
Second, design ideas
The idea of design has not yet been figured out. Can only look at the classmate's train of thought.
The design of the program is generally, first set 2 variables, the first number is temporarily given to shuiwang this variable and the next number, if the same with an X variable from one, the difference is reduced by one; so loop, if you encounter different, always minus minus, X becomes 0,shuiwang and is overwritten by new number , and so on, find the ultimate water King ID.
Third, the source code
1#include <iostream.h>2 3 intFindshuiwang (intArrid[],intnum)4 {5 intShuiwang;6 intx;7 for(inti=x=0; i<num;i++)8 {9 if(x==0)Ten { Oneshuiwang=arrid[i],x=1; A } - Else - { the if(shuiwang==Arrid[i]) - { -++x; - } + Else - { +--x; A } at } - } -cout<< ("The ID of this "water King" is:"); -cout<< ("%d\n", Shuiwang); - returnShuiwang; - } in - to + intMain () - { the intarry[6]={222,333,333,333,444,444,}; *Findshuiwang (Arry,6); $ return 0;Panax Notoginseng}
Four, the experiment
Five, the experiment Summary:
The classroom training and wood have to understand. But because I slept. Afraid of not pay homework, so can only see a classmate of the train of thought, the basic approximate code or classmate, a little optimization, a bit, the feeling is OK, really according to the teacher said this idea code is very simple. Although there is still a little to understand.
Find "Water king" in class practice