Find "Water king" in class practice

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.