1.报数游戏,输入一个整数m,有1<=n<=100个人参与报数游戏 ,从1,2,,,m报数,报m的人淘汰,下一轮接着从1开始报数到m,一直循环,直到剩下的人数小于m时停止。输入:m 输出:最终剩下的m-1个数。
例:输入:3 输出:58,91(循环链表也能做)
public class leetcode1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int totalNum = 100;
int cycleNum = scanner.nextInt();
yuesefu(totalNum, cycleNum);
scanner.close();
}
public static void yuesefu(int totalNum, int cycleNum) {
// 初始化人数
List<Integer> start = new ArrayList<Integer>();
for (int i = 1; i <= totalNum; i++) {
start.add(i);
}
List<Integer> end = new ArrayList<Integer>();
// 从第K个开始计数
int k = 0;
while (start.size() > 0) {
k = k + cycleNum;
// 第m人的索引位置
k = k % (start.size()) - 1;
// 判断是否到队尾
if (k < 0) {
end.add(start.get(start.size() - 1));
start.remove(start.size() - 1);
k = 0;
} else {
end.add(start.get(k));
start.remove(k);
}
if(end.size()==100){
while (100-cycleNum<=100){
Integer i= end.get(100-cycleNum+1);
cycleNum--;
System.out.print(i);
if((100-cycleNum+1)<=99)System.out.print(",");
if((100-cycleNum+1)==100)
break;
}
}
}
}
}
2.大概是这样,无源码(可以用层序遍历做,偶层反转,奇层不反转)
给定一个数字n,1<=n<=30 例:输入2 输出: 1*** 3*** 2*** 规则:每行不足四位数用*补全 奇数行顺序排,偶数行逆序排,
3.隐约记得是个棋盘类题