有10个人围成一-圈,顺序排号。从第一个人开始报数(从1到3报数),凡报到3的人退出圈子,问最后留下的是第几号的人。
2条回答 默认 最新
- 五一编程 2021-10-23 14:17关注
public class LeftNumber {
public static void main(String[] args) {
int value = LeftNumber(1);
System.out.println(value);
}
public static int LeftNumber(int count) {
if (count <= 0) {
throw new IllegalArgumentException("人数必须大于0");
}
boolean[] status = new boolean[count];
for (int i = 0; i < status.length; i++) {
status[i] = true;
}
int index = -1;
int counter = 0; //当前退出的人数 n-1
int current = 0; //当前人报的数
while (counter < status.length - 1) {
//index 成环
index = (index + 1) % status.length;//0
if (!status[index]) {
continue;
}
//报数
current = (current + 1) % 3; //1
//退出
if (current == 0) {
counter++;
status[index] = false;
}
}
for (int i = 0; i < status.length; i++) {
if (status[i]) {
return i + 1;//1-n 0-(n-1)
}
}
throw new RuntimeException("未找到!!!");
}
}本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 win10权限管理,限制普通用户使用删除功能
- ¥15 minnio内存占用过大,内存没被回收(Windows环境)
- ¥65 抖音咸鱼付款链接转码支付宝
- ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
- ¥15 求螺旋焊缝的图像处理
- ¥15 blast算法(相关搜索:数据库)
- ¥15 请问有人会紧聚焦相关的matlab知识嘛?
- ¥15 网络通信安全解决方案
- ¥50 yalmip+Gurobi
- ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面