代码拉取完成,页面将自动刷新
package second_term.sixth_chapter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Josephus {
/**
* Continue around the circle eliminating every nth soldier
* until all of the soldiers have been eliminated.
*/
public static void main(String[] args)
{
int numPeople, skip, targetIndex;
List<String> list = new ArrayList<String>();
Scanner in = new Scanner(System.in);
// get the initial number of soldiers
System.out.print("Enter the number of soldiers: ");
numPeople = in.nextInt();
in.nextLine();
// get the number of soldiers to skip
System.out.print("Enter the number of soldiers to skip: ");
skip = in.nextInt();
// load the initial list of soldiers
for (int count = 1; count <= numPeople; count++)
{
list.add("Soldier " + count);
}
targetIndex = skip-1;
System.out.println("The order is: ");
// Treating the list as circular, remove every nth element
// until the list is empty
while (!list.isEmpty())
{
System.out.println(list.remove(targetIndex));
if (list.size() > 0)
targetIndex = (targetIndex + skip-1) % list.size();
}
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。