Find the Water King 2

Source: Internet
Author: User

One: Programming background

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: Design ideas

This problem is similar to looking for water King 1, except that the elimination from 1:1 becomes 3:1 elimination, and the other count additions are similar.

Three: Code implementation

//Find the Water King 2//Letter 1405-1 class 20142966 Huang WeipengImportJava.util.Scanner; Public classFind2 { Public Static voidMain (string[] args) {        intI,number; System.out.println ("Please enter the total number of posts:"); Scanner in=NewScanner (system.in); number=In.nextint (); intlist[]=New int[number];//define an array of length for the post        intcount[]=New int[3];//defines an array of three water king repeating values that are counted separately        intmiddle[]=New int[3];//defines an intermediate array that assigns three water kings to each otherSystem.out.println ("Please enter the ID of the post"); Scanner T=NewScanner (system.in);  for(i=0;i<number;i++)//Assigning a post ID to an array{List[i]=T.nextint (); }         for(i=0;i<3;i++)//initialize a count array{Count[i]=0; }         for(i=0;i<number;i++)        {            if(middle[0]==List[i]) {count[0]++; }            Else if(middle[1]==List[i]) {count[1]++; }            Else if(middle[2]==List[i]) {count[2]++; }            Else if(count[0]==0) {count[0]++; middle[0]=List[i]; }            Else if(count[1]==0) {count[1]++; middle[1]=List[i]; }            Else if(count[2]==0) {count[2]++; middle[2]=List[i]; }            Else{count[0]--; count[1]--; count[2]--; }} System.out.println ("The first water King's ID is:" +middle[0]); System.out.println ("The second water King's ID is:" +middle[1]); System.out.println ("The third water King's ID is:" +middle[2]); }}

Four: Realization

Five: Personal Summary

There are several ways to achieve this problem, but the problem requires a quick find that the algorithm is simple, as long as the number of posts the most ID can be found.

Find the Water King 2

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.