Search for Shui Long Wang

Source: Internet
Author: User

Design Philosophy:

By"This "Water King" has more than half the number of posts"We can know that when two adjacent IDs are usedThe ID of the Water Dragon KingWill not be completely eliminated, that is, the total number of dragons will not be reduced to 0; Use the array longid []Record all IDs, Assume that the first is the Dragon King, and the second IDComparison. If they are the same, longwang IDAdd one to the total number; otherwise, subtract one until it is reduced to 0., Then change the Dragon King ID, Always loop until the end.

Code implementation:

ImportJava. util. vendor;

Public ClassLongwang {

Public Static VoidMain (string [] ARGs ){

Scanner reader =NewUsing (system.In);

System.Out. Println ("Enter the total number of IDS :");

IntSum = reader. nextint ();

IntId [] =New Int[Sum]; // array ID [] stores all IDs

System.Out. Println ("Enter ID :");

For(IntI = 0; I <sum; I ++)

{

Id [I] = reader. nextint ();

}

IntLongid = ID [0];IntJ = 1; // assume that the first ID is the ID of the Water Dragon King. j indicates the total number of records of the Dragon King ID.

For(IntI = 1; I <sum; I ++)

{

If(Longid! = ID [I]) // The Dragon King ID is compared with the last one. If the difference is not the same, the total number of Dragon King IDS is reduced by one.

{

J = J-1;

If(J <= 0) // when the total number of Dragon King IDS is 0, change the Dragon King ID and compare it with the last Id.

{

Longid = ID [I + 1];

J = 1;

I ++;

}

}

Else// When the Dragon King ID is equal to the next ID, the total number of Dragon King IDs plus one

{

Longid = ID [I];

J = J + 1;

}

}

System.Out. Println ("the ID of the Water Dragon King is:" + longid );

}

}

 

Implementation:

Personal summary:

Try to find a simple method before programming, first think about the logical structure, then programming.

Search for Shui Long Wang

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.