CCF认证201712-2游戏
- 问题描述
- 有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。
思路:使用队列实现。
- import java.util.ArrayDeque;
- import java.util.Queue;
- import java.util.Scanner;
- public class Main_Game {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int count = sc.nextInt();// 小朋友个数
- int num = sc.nextInt();// 需要数的数
- int currnum = 1;
- Queue<Integer> queue = new ArrayDeque<Integer>();// 小朋友组成的队列
- for (int i = 1; i <= count; i++) {
- queue.add(i);
- }
- while (queue.size() > 1) {
- int top = queue.element();//队首元素
- queue.remove();//删除队首元素
- if(currnum % num !=0 && currnum %10 != num){
- //如果不是num的倍数并且不包含num
- queue.add(top);//将元素加入队尾
- }
- currnum ++;
- }
- System.out.println(queue.element());
- }
- }
链表实现:
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Scanner;
- public class CountGame {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int count = sc.nextInt();// 小朋友个数
- int num = sc.nextInt();// 需要数的数
- int currnum = 0;
- int currid = 0;
- List<Person> pers = new LinkedList<Person>();
- for(int i = 0; i < count; i++){
- Person pes = new Person(i+1);
- pers.add(pes);
- }
- while (pers.size() > 1) {
- currnum ++;
- if (currnum % num != 0 && currnum%10!=num) {
- // 数到不是num的倍数也不包含num的数
- pers.get(currid).setNumber(currnum);
- }else{
- pers.remove(currid);
- if(currid >= pers.size()){
- currid = 0;
- }
- continue;
- }
- if(currid < pers.size()-1){
- currid++;
- }else{
- currid = 0;
- }
- }
- System.out.println(pers.get(0).getId());
- }
- }
- class Person {
- private int id;
- private int number;
- public Person(){}
- public Person(int id){
- this.id = id;
- }
- public Person(int id,int number){
- this.id = id;
- this.number = number;
- }
- public int getId() {
- return this.id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public int getNumber() {
- return number;
- }
- public void setNumber(int number) {
- this.number = number;
- }
- }
CCF认证201712-2游戏的更多相关文章
- CCF认证历年试题
CCF认证历年试题 不加索引整理会死星人orz 第一题: CCF201712-1 最小差值(100分) CCF201709-1 打酱油(100分) CCF201703-1 分蛋糕(100分) CCF2 ...
- 小明种苹果(续)第十七次CCF认证
小明种苹果(续)第十七次CCF认证 题目 原题链接 ](http://118.190.20.162/view.page?gpid=T93) 很高心,在现在CCF CSP可以下载自己当时的答卷了,也就是 ...
- CCF认证(1)
#include <iostream> #include <windows.h> using namespace std; typedef struct letter{ int ...
- CCF 认证4
题意:求强联通分量 Tarjan算法 #include<iostream> #include<stdio.h> #include<stdlib.h> #includ ...
- CCF 认证
题意:字符串替换 string+map的应用 #include<iostream> #include<stdio.h> #include<stdlib.h> #in ...
- CCF认证考试——折点计数
描述:简单题 #include<iostream> using namespace std; int main() { ], n, count = ; cin >> n; ; ...
- CCF认证之——相反数
这道题目非常简单! #include<iostream> using namespace std; int main() { ],n,count=; cin >> n; ; i ...
- ccf认证 201709-4 通信网络 java实现
试题编号: 201709-4 试题名称: 通信网络 时间限制: 1.0s 内 ...
- ccf认证模拟题之三---最大的矩形
问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n)个矩形的高度是hi.这n个矩形构成了一个直方图.例如,下图中六个矩形的高度就分别是3, 1, 6, 5, 2, 3 ...
随机推荐
- 阿里云短信服务Java版
短信服务管理平台 https://dysms.console.aliyun.com/dysms.htm java短信发送API https://help.aliyun.com/document_ ...
- Hive安装与应用过程
1. 参考说明 参考文档: https://cwiki.apache.org/confluence/display/Hive/GettingStarted 2. 安装环境说明 2.1. 环境说明 ...
- Difference between model.evaluate vs model.predict in Keras
The model.evaluate function predicts the output for the given input and then computes the metrics ...
- arcgis JavaScript 加载 mapbox地图
mapbox 地图现在是越来越好看了, 随便试 /** * Created by Administrator on 2018/5/15 0015. */ import * as esriLoader ...
- 调用Android中的软键盘
我们在Android提供的EditText中单击的时候,会自动的弹 出软键盘,其实对于软键盘的控制我们可以通过InputMethodManager这个类来实现.我们需要控制软键盘的方式就是两种一个是像 ...
- linux 文件操作命令 touch、cat、more、less、head、tail
touch /bin/touch 创建空文件 linux 创建文件可以使用特殊符号,/除外 touch test test1 创建了两个文件touch "test test1" 创 ...
- WiFi 干扰器,有时间可以去试试呦!
转自社区: 0X01 引言 想不想搞个WIFI干扰器?网上搜集了一下资料,发现用esp8266可以实现简单的干扰功能,包括断网.复制.欺骗等等.刚好手上有块Tpyboard V202(30元),也是e ...
- ssm单项目整合
目录 前言 创建maven项目 添加依赖 配置文件 总览 jdbc配置 mybatis配置 dao层配置 service层配置 事务配置 controller配置 web.xml 使用 前言 spri ...
- 记开发个人图书收藏清单小程序开发(十)DB开发——新增图书信息
昨晚完成了Web端新增图书信息的功能,现在就差DB的具体实现了. 因为我把Book相关的信息拆分的比较多,所以更新有点小麻烦. 首先,我需要创建一个Book Type的Matter: 然后,将图片路径 ...
- Tomcat6.0下的jsp、servlet和javabean的配置
第一步:下载jdk和tomcat: 第二步:安装和配置你的jdk和tomcat:执行jdk和tomcat的安装程序,然后设置按照路径进行安装即可.1.安装jdk以后,需要配置一下环境变量,在我的电脑- ...