Java解决小孩围圈问题
2024-09-19 23:56:28
问题描述:一堆小孩围成一个圈,从第一个小孩开始数,每数到第三个便把第三个孩子删去,数到只剩一个小孩为止,并求该孩子的具体编号。
解决办法
1.
package test;
public class Compare {
public static void main(String[] args) {
boolean[] array=new boolean[];
for(int i=;i<array.length;i++) {
array[i]=true;
}
int leftcount = ;
int countnum = ;
int index = ; while(leftcount > ) {
if(array[index]) {
countnum ++;
if(countnum == ) {
countnum = ;
array[index] = false;
leftcount--;
}
}
index ++;
if(index == ) {
index = ;
}
}
for(int i=;i<array.length;i++) {
if(array[i]) {
System.out.println(i);
}
}
} }
2.
package test; public class Studentcircle {
public static void main(String[] args) {
makecircle b=new makecircle();
int countnum=;
student m=b.first;
while(b.count>) {
countnum++;
if(countnum==) {
countnum=;
b.delete(m);
}
m=m.right;
}
System.out.println(b.first.id);
} }
class student{
int id;
student left;
student right;
}
class makecircle{
int count=;
student first,last;
makecircle(int n){
for(int i=;i<n;i++) {
add();
}
}
void add() {
student x= new student();
x.id=count;
if(count<=) {
first=x;
last=x;
x.left=x;
x.right=x;
}else {
last.right=x;
x.left=last;
x.right=first;
first.left=x;
last=x;
}
count++;
}
void delete(student x) {
if(count<=) {
return;
}else if(count==) {
first=last=null;
}else {
x.left.right=x.right;
x.right.left=x.left; if(x==first) {
first=x.right;
}else if(x==last) {
last=x.left;
}
}
count --;
} }
最新文章
- ABP架构设计交流群-上海线下交流会的内容分享(有高清录像视频的链接)
- c# socket
- C#串口通讯实例
- 写在学AngularJS之前
- [BZOJ1116][Poi2008]LCO(并查集)
- 移动端bug总结
- word-break:break-all和word-wrap:break-word的区别
- lintcode:Subtree 子树
- CentOS Nginx+jdk+tomcat 环境搭建
- 移动web:翻页场景动画
- Hadoop化繁为简-从安装Linux到搭建集群环境
- 计算机网络之套接字SOCKET
- 未能加载文件或程序集“file:///C:\Program Files (x86)\SAP BusinessObjects\Crystal Reports for .NET Framework 4.0
- swiper 自定义分页器的使用
- [Unity][安卓]Unity和Android Studio 3.0 交互通讯(1)Android Studio 3.0 设置
- hdu 1263 水果 【二维map】
- 黄聪:OTP动态密码_Java代码实现
- 使用Qt开发绘制多个设备的流量曲线图(附带项目图)
- java框架篇---hibernate(一对一)映射关系
- 【巷子】---json-server---基本使用
热门文章
- Java GC2
- Session establishment complete on server 2181, sessionid = 0x35fb853eb6f0004
- courator - create
- Java - 15 Java 正则表达式
- Heartbeat+DRBD+MFS高可用
- android:clipChildren属性,子布局超出父布局;
- call与apply简单介绍
- 深入Spring Boot:怎样排查 Cannot determine embedded database driver class for database type NONE
- 解决从客户端(Content=";<;div>;<;p >;<;p>;12312...";)中检测到有潜在危险的Request.Form 值。
- ES6入门声明