Use Java to solve outgoing problems in sequence

Source: Internet
Author: User

 

/**
* Using Java to solve outgoing problems in sequence-Author: Wen min, CTO of Chenzhou
*------------------------------------------------------------------------------
* Problem Description
* There are n people sitting around, numbered 1. N,
* When reporting data from 1 to M, this person is out; when reporting data from 1 to m, the other person is out.
* In this loop and so on, calculate:
* Number of the person who finally leaves the game
* Display the numbers of each reporter in sequence
* Solutions
* Develop an algorithm. The algorithm is located in an array. Each time it is estimated from N1, a non-zero one is found,
* Assume that a number is reported, and m is reported. If an employee is found
* Store the number at this position in the array to another array, and set it to 0 to indicate that the array is out.
* The algorithm is constantly called until the last speaker is found.

*/


Class test {
Int [] Nums;
Int [] numsbk;
Int N1; // the previous one at the start of each search
Int N2; // location where the employee ID is stored in the numsback Array
Int OK; // number of players found
Int incret; // condition for outbound traffic (when the number of outgoing traffic points is reached, the number of outgoing traffic is exceeded)
 
Final int inicnt = 15; // array size, change it to change the number of people
 
// Initialize data
Public test (){
Nums = new int [inicnt];
Numsbk = new int [inicnt]; // The numsbsk array will store each outbound number in sequence

// The Nums array stores the number of each person. The number starts from 1 to inicnt.
For (INT I = 1; I <= inicnt; I ++ ){
Nums [I-1] = I;
}
N1 =-1; // for the first time, it will start from 0. Therefore, set N1 to-1 so that it can increase by 1 to 0.
N2 = 0; // The employee ID found for the first time will be saved to the first position of numsbk, so N2 is set to 0
OK = 0; // no one is logged out yet, so OK is set to 0
Incret = 4; // This person is out of the game every time he points to 4.
}
 
Public void findnext (){
Int CNT = 0;
While (true ){
N1 ++;
If (n1 = inicnt) // find the one after the last one and it should be 0 (go back)
N1 = 0;

If (Nums [N1]! = 0) {// = 0, so it is out! = 0
CNT ++; // find one! A value of 0 indicates the number of vertices.
/**
* If the number of incres has been found, this person should be out.
* Store the employee number at numsbsk [n2], and then
* Add N2 to 1 to store the outgoing phone number next time.
* Setting Nums [N1] to 0 indicates that this person is out, and then
* "OK" and "1" indicate that another player has been found.
*/
If (CNT = incres ){
Numsbsk [n2] = Nums [N1];
N2 ++;
Nums [N1] = 0;
OK ++;
Break;
}
}
}


If (OK <inicnt) // if no reporter is found, call the API repeatedly to find the next reporter.
Findnext ();
Else {
System. Out. println ("final performer:" + numsbak [inicnt-1]);
System. Out. println ("----------------------------------------");
System. Out. println ("outgoing in turn :");
// The following figure shows the number of outgoing personnel in sequence.
For (INT I = 0; I <inicnt; I ++ ){
System. Out. println (numsbk [I]);
}
}

}
 
Public static void main (string [] ARGs ){
Test test = new test ();
Test. findnext ();
}
}

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.