1. Requirements: A group of people in a circle to count, every 3 back 1 people, asked to calculate the last left of the subscript of the People
2. Code:
1 PackageTest;2 3 Public classcount3quit1 {4 5 //requirements: A group of people in a circle to count, every 3 back 1 people, asked to calculate the last left of the subscript of the People6 7 Public Static voidMain (string[] args) {8 9 //receive Java parameters specify number of peopleTen intLen = Integer.parseint (args[0]); One A //simulates a circle of people with a Boolean array, True indicates still, false indicates exited - Boolean[] Peoples =New Boolean[Len]; - /*For (Boolean b:peoples) { the B = true; - }*/ - for(inti = 0; i < peoples.length; i++){ -Peoples[i] =true; + } - + A //every 3 back 1 at intLeftcount = Len;//1.leftCount says how many people are left now - intindex = 0;//2.index Current Element subscript - intCount = 0;//3.count means how many to count - - while(Leftcount > 1){ - if(Peoples[index]) { incount++; - if(Count = = 3){ toPeoples[index] =false; +Count=0; -leftcount--;//minus 1 for the rest of the population. the } * } $ Panax Notoginseng //put the element under the label -index++; the + //If you have counted to the end of the array, start the number A if(Index > Len-1){ theindex = 0; + } - } $ $ for(inti = 0; i < peoples.length; i++){ - if(Peoples[i]) { - System.out.println (i); the } - }Wuyi } the}
3. Running results Java COUNT3QUIT1 500:
435
java-data structures and algorithms-every 3 minus 1