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