Personal assignment-Find the Water King 2

Source: Internet
Author: User

Title Requirements:

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?

Design ideas:

1. Enter the total number of posts x

2. Define a one-dimensional array a[x], which is the author ID. Set Loop input author ID

3. Definition of three kings, and their net posts

4, then set a cycle, in order to compare, if the number of net posts is greater than 0 , the author ID is unchanged, if the author ID equals 0 , this ID is used as the new Aqua King ID, so it loops x times

5, the last remaining author id is the water king ID

SOURCE program:

1 ImportJava.util.Scanner;2 3 4  Public classWater {5 6      Public Static voidMain (string[] args) {7         //TODO Auto-generated method stubs8SYSTEM.OUT.PRINTLN ("Total number of posts (please enter more than 3)");9Scanner input =NewScanner (system.in);Ten         intx=input.nextint (); OneString a[]=NewString[x]; A         //Posting Status -          for(inti=0;i<x;i++) -         { theSystem.out.println ("Please enter the author ID of" + (i+1) + "POST"); -Scanner INPUT1 =NewScanner (system.in); -a[i]=input1.nextline (); -         } +         //Find the Water king -String s1= "";//Aqua King 1 +String s2= "";//Aqua King 2 AString s3= "";//Aqua King 3 at         intC1=0; -         intC2=0; -         intc3=0;//c1,c2,c3 are small Wang Wang the last net post number, if C1,C2,C3 is not a positive is not water King -          for(inti=0;i<x;i++) -         { -            if(S1.equals (A[i]))//if the number of equal net posts plus 1 in            { -c1++; to            } +            Else if(S2.equals (A[i])) -            { thec2++; *            } $            Else if(S3.equals (A[i]))Panax Notoginseng            { -c3++;  the            } +            Else if(c1==0) A            { thes1=A[i]; +            } -            Else if(c2==0) $            { $S2=A[i]; -            } -            Else if(c3==0) the            { -s3=A[i];Wuyi            } the            Else//if both are unequal the number of net posts is reduced by 1 -            { Wuc1--; -c2--; Aboutc3--;  $            } -         } -System.out.println ("Water King" is "+s1+" "+s2+" "+S3); -     } A  +}

Run:

Results Analysis: this time to find three of the small, in order to avoid duplication, so the initialization to be carried out through the loop, the three kings in the order of the loop to initialize the first occurrence of the three different IDs, so that the number of net posts can not be initialized to 1, to be initialized to 0, You can set the Wang value to 1 only if it is a small aqua.

Personal assignment-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.