Ai
2 Star 0 Fork 0

CS-IMIS-23/20172309_javaProgramming

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Josephus.java 1.30 KB
一键复制 编辑 原始数据 按行查看 历史
20172309 提交于 2018-10-06 10:48 +08:00 . 书上例子Josephus
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();
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Java
1
https://gitee.com/CS-IMIS-23/20172309_javaProgramming.git
git@gitee.com:CS-IMIS-23/20172309_javaProgramming.git
CS-IMIS-23
20172309_javaProgramming
20172309_javaProgramming
master

搜索帮助