游戏

试题编号: 201712-2
试题名称: 游戏
时间限制: 1.0s
内存限制: 256.0MB
问题描述:
问题描述
  有n个小朋友围成一圈玩游戏,小朋友从1至n编号,2号小朋友坐在1号小朋友的顺时针方向,3号小朋友坐在2号小朋友的顺时针方向,……,1号小朋友坐在n号小朋友的顺时针方向。
  游戏开始,从1号小朋友开始顺时针报数,接下来每个小朋友的报数是上一个小朋友报的数加1。若一个小朋友报的数为k的倍数或其末位数(即数的个位)为k,则该小朋友被淘汰出局,不再参加以后的报数。当游戏中只剩下一个小朋友时,该小朋友获胜。
  例如,当n=5, k=2时:
  1号小朋友报数1;
  2号小朋友报数2淘汰;
  3号小朋友报数3;
  4号小朋友报数4淘汰;
  5号小朋友报数5;
  1号小朋友报数6淘汰;
  3号小朋友报数7;
  5号小朋友报数8淘汰;
  3号小朋友获胜。
  给定n和k,请问最后获胜的小朋友编号为多少?

输入格式
  输入一行,包括两个整数n和k,意义如题目所述。

输出格式
  输出一行,包含一个整数,表示获胜的小朋友编号。

样例输入
5 2
样例输出
3
样例输入
7 3
样例输出
4
数据规模和约定
  对于所有评测用例,1 ≤ n ≤ 1000,1 ≤ k ≤ 9。

题目分析:使用队列

代码:

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
int n , k;

bool flag(int count){
	if(count % k == 0){
		return true;
	}else if((count % 10) == k){
		return true;
	}else{
		return false;
	}
}

int main(){

	queue<int> que;
	while(cin >> n >> k){
		for(int i = 1 ; i <= n; i++){
			que.push(i);
		}
		int head = 0;
		int count = 0;
		while(!que.empty()){
			head = que.front();
			que.pop();
			count++;
			if(flag(count) == false){      //不是报的数字,再进去 
				que.push(head);
			}
		}
		cout << head << endl;
	}
    return 0;
}

/*第二次 

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<fstream>
#include<iostream>
#include<iomanip>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;

int main()
{
	queue<int> qu;
	int n , k;
	while(cin >> n >> k){
		for(int i = 1 ; i <= n ; i++){
			qu.push(i);
		}
		int t = 1;
		while(qu.size() > 1){
			int a = qu.front();
			qu.pop();
			if(t % 10 != k && t % k != 0){
				qu.push(a);
			}
			t++;
		}
		cout << qu.front() << endl;
		qu.pop();
	}	
    return 0;
}


*/ 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值