4 The ones to remain

描述
There are N soldiers standing in one line. They are marked from 1 to N, from right to left. And they are given a number
m. Then the soldiers numbered off, straight from the right-hand man. The one who reported a number that is the multiple
of m was kept in the line. Others have to leave the line. They continue doing this till the number of people in the line
is less than m. For example, if there are 10 soldiers, and m = 3. For the first time the soldiers who are marked 3, 6,
9 remain in the line. For the second time the soldier who is marked 9 remains in the line. Because the number of soldiers
in the line is less than m, so the soldier marked 9 was the only one to remain in the line.
Now we want to know who will be the ones to remain, can you tell us ?
输入
There are several test cases in the input. Each test cases is only one line, contains two integers n and m.(3 <= n <=
109, 2 <= m <= n). The input ends when n = 0 and m = 0.
输出
For each test case, output two lines. The first line contains one integer x, the number of soldiers to remain. The second
line contains x integers, the numbers marked on the soldiers who remain in the line. You should output them in increasing
order.
样例输入
10 3
8 3
0 0
样例输出
1
9
2
3 6
翻译:描述
有 N 个士兵站在一行。他们被从右到左标记为 1 到 N。他们被给与了一个数字 m。然后士兵直接从右面报数。报的数是 m的倍数的留下
来,其他人离开。然后继续上述操作,直到人数少于 m。例如,有 10 个士兵, m=3。第一次士兵报数为 3 6 9 的留下,第二次士兵报数
为 9 的留下。
输入
有多组测试数据。每组一行两个数 n m(3 <= n <= 109, 2 <= m <= n) ,以 0 0 结束
输出
每组输出两行,第一行输出一个 x 表示留下来的士兵数量,第二行输出 x 个留下来的士兵的编号
#include<stdio.h>

#define N 110
int in_queue[N];
int main(){
	int n,m;
	while(scanf("%d %d",&n,&m) && n!=0 && m != 0){
		for(int i=1;i<N;i++){//给队中的人标记编号
			in_queue[i] = i;
		}
		int len = n;//len表示实时排队的人数
		int j,i;
		while(len >= m){
			for(i=1,j=1;i<=len;i++){
				if(i%m==0){//如果报到的数是倍数 
					in_queue[j]=in_queue[i];//记录需要保留下来的人的编号
					j++;//j动态增长,以表示队中人的个数
				}
			}
			len = j - 1;//根据实际的人数设定len,之前多加了一个,现在再回复。
		}
		printf("%d\n",len);//输出队中人的个数
		for(int i=1;i<=len;i++){
			printf("%d ",in_queue[i]);//输出队列中剩余人的编号
		}
		printf("\n");
	}
	return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值