报数游戏
标题:报数游戏 | 时间限制:1秒 | 内存限制:262144K | 语言限制:不限
100个人围成一圈,每个人有一个编码,编号从1开始到100。他们从1开始依次报数,报到为M的人自动退出圈圈,然后下一个人接着从1开始报数,直到剩余的人数小于M。请问最后剩余的人在原先的编号为多少?
def func(M): _list = [] for i in range(1, 101): _list.append(i) i=0 n=100 count=1 while n > M-1: m=0 for i in range(0,n): if count%M!=0: _list[m]=_list[i] m+=1 count+=1 n=m res=_list[:M-1] res.sort() return res M = int(input()) if M<=1 or M>=100: print("ERROR!") else: print(",".join([ str(i) for i in func(M)]))
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ; import com.sun.org.apache.bcel.internal.generic.INEG; import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner =new Scanner(System.in); int num = Integer.valueOf(scanner.nextLine()); if (num>=100||num<1){ System.out.println("ERROR!"); return; } List<Integer> nums = new ArrayList<>(); for (int i =1;i<=100;i++){ nums.add(i); } int han = 1; int numNow=0; while (nums.size()>=num){ if (numNow==nums.size()){ numNow=0; } if (han==num){ han=1; nums.remove(numNow); continue; } numNow++; han++; } StringBuilder sb = new StringBuilder(); for (int nn:nums){ sb.append(nn).append(","); } if (sb.length()!=0){ sb.setLength(sb.length()-1); } System.out.println(sb.toString()); } }