定义人类结构体
public class people {
int num;
people next;
public people(int num) {
this.num = num;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public people getNext() {
return next;
}
public void setNext(people next) {
this.next = next;
}
}
核心代码
public class JosefulLikeList {
people head;
public void addpeople(int num)
{
if(num<1)
{
System.out.println("人数不符合开始规定");
return;
}
people pos=null;
for(int i=1;i<=num;i++)
{
people people= new people(i);
if(i==1)
{
head = people;
pos=head;
head.next=head;
}
else
{
pos.next=people;
people.next=head;
pos=people;
}
}
}
public void showLikelist()
{
if(head.next==null)
{
System.out.println("不符合规定");
return;
}
people pos=head;
while(true)
{
System.out.println(pos.num);
if(pos.next==head)
{
break;
}
pos=pos.next;
}
}
public void chuquan(int startnum,int all,int ci)
{
if(head.next==null||startnum<1||startnum>all)
{
System.out.println("不符合标准");
return;
}
people helper=head;
while(true)
{
if(helper.next==head)
{
System.out.println("找到了head的上一个人");
break;
}
helper=helper.next;
}
for(int i=1;i<startnum;i++)
{
head=head.next;
helper=helper.next;
}
while(true)
{
if(head==helper)
{
System.out.println(head.num);
break;
}
for(int i=1;i<ci;i++)
{
head=head.next;
helper=helper.next;
}
System.out.printf("第%d号出圈\n",head.num);
head=head.next;
helper.next=head;
}
System.out.printf("最后留下来的是%d",head.num);
}
}
测试
public class test {
public static void main(String[] args) {
JosefulLikeList josefulLikeList = new JosefulLikeList();
josefulLikeList.addpeople(5);
josefulLikeList.showLikelist();
josefulLikeList.chuquan(1,5,2);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?