一个经典编程题目,可用各种编程语言求解

如题:有17个人围成一圈(编号0~16),从第0号的人开始从1报数,凡报到3的倍数的人离开圈子,然后再数下去,直到最后只剩下一个人为止,问此人原来的位置是多少号?

使用java求解答案:
//创建学生类,设置相关属性
public class Student {
public int index=0; //位置编号
public int state=0; //活动状态
Temp t;
public Student(int index,Temp t){
this.index=index;
this.t=t;
}
public void removeStu(){
System.out.println(index+"被移除了");
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
t.stus.remove(this);
}
}


public class Temp {

Student stu;
int count=0; //人数
int num =1; //报的数
List<Student> stus=new ArrayList<Student>();

public void search(){
for(int i=0;i<17;i++){
stu=new Student(i,this);
stus.add(stu);
}

while(true){
count=stus.size();
if(count==1){
break;
}
for(int i=0;i<count;i++){
if(num%3==0){
stu=stus.get(i);
stu.state=1;
}
num++;
}

for(int i=0;i<stus.size();i++){
stu=stus.get(i);
if(stu.state==1){
stu.removeStu();
}
}
}

System.out.println("最后一个学生编号为"+stus.get(0).index);
}

public static void main(String[] args) {
Temp t=new Temp();
t.search();

}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值