Software Engineering Personal Assignment 06

Source: Internet
Author: User

First, the program title:

A three-person line designed a forum for irrigation. Information college students like to exchange irrigation above, legend in the forum there is a "water king", he not only likes to post, but also reply to other ID issued by each post. The "Water King" has been rumored to have posted more than half the number of posts. 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 statistics they have more than 1/4 posts, if you have a current forum posts (including replies) list, where the author's ID of the post is also in which you can quickly find them from the list of posts?

Second, the design idea

From the idea of looking for water king, just change the counter to three, use two one-dimensional array to represent water king and counter, in the process of one-cycle author ID, three temporary water kings were compared with the current ID, and obtained three water kings.

Third, the code implementation

Importjava.util.InputMismatchException;ImportJava.util.Scanner; Public classfinding_2 { Public Static voidMain (string[] args) {//TODO auto-generated Method Stub             for(;;)//Infinite loop According to user demand        {            intJudge=0; Scanner in=NewScanner (system.in); intN; System.out.println ("Please enter the number of posts:"); N=In.nextint (); System.out.println ("Please enter the ID (ordinal is a positive integer):"); intid[]=New int[n]; Try                              //Capturing input Errors            {                 for(inti=0;i<n;i++) {Id[i]=In.nextint (); }             }            Catch(inputmismatchexception e) {System.out.println ("The input is illegal!" Please enter an integer! "); Judge=1; }            intshuiwang[]=New int[3]; inttemp[]=New int[3];//three counters, three water kingsTemp[0]=temp[1]=temp[2]=0; shuiwang[0]=shuiwang[1]=shuiwang[2]=-1;//Initialize counter and three water King ID             for(inti=0;i<n;i++)            {                                                     if(temp[0]==0)//If temp is 0, the water King value is the current ID and the counter is incremented by 1{temp[0]++; shuiwang[0]=Id[i]; }                Else if(temp[1]==0) {temp[1]++; shuiwang[1]=Id[i]; }                Else if(temp[2]==0) {temp[2]++; shuiwang[2]=Id[i]; }                Else if(Id[i]==shuiwang[0])//When a water king is at the same time as the next ID, the counter temp+1{temp[0]++; }                Else if(id[i]==shuiwang[1]) {temp[1]++; }                Else if(id[i]==shuiwang[2]) {temp[2]++; }                Else                              //If the current water King ID is different from the current ID, the three counters are reduced by one until temp is 0{temp[0]--; temp[1]--; temp[2]--; }             }            if(judge!=1)//If no input error occurs, find the water king{System.out.println ("The Water King is:");  for(inti=0;i<3;i++) System.out.println (Shuiwang[i]); } System.out.println ("Keep looking for the water king Press any key, exit please press Q:"); String s=In.next (); if(S.equals ("Q")) System.exit (0); Else                Continue;        In.close (); }    }}

Iv. Realization OF

V. Personal SUMMARY

In the previous program to improve, to step-by-step analysis, exercise programming ability.

Software Engineering Personal Assignment 06

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.