几个人围成一圈的问题
编辑: 面试啦 发布时间: 2023-04-12 04:10:32
编程题:设有n个人依围成一圈,从第1个人开始报数,数到第m个人出列,然后从出列的下一个人开始报数,数到第m个人又出列,…,如此反复到所有的人全部出列为止。设n个人的编号分别为1,2,…,n,打印出出列的顺序;要求用java实现。【中等难度】
答:代码如下:
package test;
public class CountGame {
private static boolean same(int[] p,int l,int n){
for(int i=0;i
if(p[i]==n){
return true;
}
}
return false;
}
public static void play(int playerNum, int step){
int[] p=new int[playerNum];
int counter = 1;
while(true){
第51 页共59 页
if(counter > playerNum*step){
break;
}
for(int i=1;i
while(true){
if(same(p,playerNum,i)==false) break;
else i=i+1;
}
if(i > playerNum)break;
if(counter%step==0){
System.out.print(i + ” “);
p[counter/step-1]=i;
}
counter+=1;
}
}
System.out.println();
}
public static void main(String[] args) {
play(10, 7);
}
}
答:代码如下:
package test;
public class CountGame {
private static boolean same(int[] p,int l,int n){
for(int i=0;i
return true;
}
}
return false;
}
public static void play(int playerNum, int step){
int[] p=new int[playerNum];
int counter = 1;
while(true){
第51 页共59 页
if(counter > playerNum*step){
break;
}
for(int i=1;i
if(same(p,playerNum,i)==false) break;
else i=i+1;
}
if(i > playerNum)break;
if(counter%step==0){
System.out.print(i + ” “);
p[counter/step-1]=i;
}
counter+=1;
}
}
System.out.println();
}
public static void main(String[] args) {
play(10, 7);
}
}
上一篇:康拓普公司Java笔面试 下一篇:会话Bean的种类
面试题库导航
- C++面试题
- JAVA面试题
- Oracle面试题
- MySQL面试题
- Linux面试题
- 网络工程师面试题
- 网管面试题
- 软件工程师面试题
- PHP面试题
- .net面试题
- 软件测试面试题
- EJB面试题
- Spring面试题
- Javascript面试题
- Python面试题
- QTP面试题
- Servlet面试题
- J2EE面试题
- Database面试题
- ASP面试题
- Unix面试题
- Loadrunner面试题
- 通信面试题
- 嵌入式面试题
- Ruby面试题
- Delphi面试题
- Mobile开发面试题
- SOA面试题
- JAVA面试题面试题
- 研发工程师面试题
- 软件架构师面试题
- 系统工程师面试题
- Android面试题
- JAVA程序员
- C#面试题