题目标题:
报数游戏
题目描述:
有n(n<=100)围成一圈,顺序排号(从1排到n)。从第一个人开始报数(从1一直往上报数),凡报到m及m的倍数或者尾数为m的人退出圈子,问最后留下的是原来第几号的那位?
输入描述:
输入为两个正整数,第一个<=100,第二个<=9;
输出描述:
输出为一个正整数;
样式输入:
10 3
样式输出:
5
public int baoshu2(int n,Integer m){
if(n==1 || m==1){
return n;
}
// 构造一个数组存储所有n 1,2,3.. n
ArrayList<Integer> list = new ArrayList<>();
for (int i=1;i<=n;i++){
list.add(i);
}
// 构造一个全局计数 从1 开始 一直数 知道 list的长度为1
Integer count = 1;
while (list.size() >1){
int tmpSize;
tmpSize = list.get(0);
list.remove(0);
if(count%m == 0 || count.toString().endsWith(m.toString())){ // 当前报的数 为 m的倍数 或者 以m结尾
}else {
list.add(tmpSize);
}
count ++;
}
return list.get(0); n, m = map(lambda a:int(a), input().split(' ')) k = [] h = [] count = 1 for i in range(n): k.append(i+1) h.append(i + 1) while(len(k)!=1): k = h h = [] for i in range(len(k)): if(count!=3 and count%3 !=0 and count%10 !=3): h.append(k[i]) # print(count, h) count += 1 else: count += 1 print(k[0])
function baoshu(a, b) {
let arr = [];
for (let i = 1; i <= a; i++) {
arr.push(i);
}
let count = 1;
let loop = list => {
let newArr = [];
list.forEach((el, index) => {
if (canIn(count, b)) {
newArr.push(el);
}
count++;
});
if (newArr.length == 1) {
console.log('答案' + newArr);
} else {
loop(newArr);
}
};
loop(arr);
}
function canIn(a, b) {
if (a == b) {
return false;
}
if (a % b == 0) {
return false;
}
let num = Math.pow(10, a.toString().length - 1);
if (a % num == b) {
return false;
}
return true;
}
baoshu(10, 3); // 5
#include<stdio.h>
int main()
{
int i,n,m,exit=0,count=0;
int a[105]={0};
scanf("%d%d",&n,&m);
if(n<=100&&m<=9)
{
for(i=1;i<=n;i++)
a[i]=i;
i=1;
while(n-exit>1)
{
while(a[i]==0)
{
if(i==n)
i=1;
else i++;
}
count++;
if(count%m==0||count%10==m)
{exit++;a[i]=0;}
i++;
if(i==n+1)
i=1;
}
for(i=1;i<=n;i++)
if(a[i]!=0)
{
printf("%d",a[i]);
break;
}
}
return 0;
}