1. 问题描述
  2.   n个小朋友围成一圈玩游戏,小朋友从1n编号,2号小朋友坐在1号小朋友的顺时针方向,3号小朋友坐在2号小朋友的顺时针方向,……,1号小朋友坐在n号小朋友的顺时针方向。
  3.   游戏开始,从1号小朋友开始顺时针报数,接下来每个小朋友的报数是上一个小朋友报的数加1。若一个小朋友报的数为k的倍数或其末位数(即数的个位)为k,则该小朋友被淘汰出局,不再参加以后的报数。当游戏中只剩下一个小朋友时,该小朋友获胜。
  4.   例如,当n=5, k=2时:
  5.   1号小朋友报数1
  6.   2号小朋友报数2淘汰;
  7.   3号小朋友报数3
  8.   4号小朋友报数4淘汰;
  9.   5号小朋友报数5
  10.   1号小朋友报数6淘汰;
  11.   3号小朋友报数7
  12.   5号小朋友报数8淘汰;
  13.   3号小朋友获胜。
  14.  
  15.   给定nk,请问最后获胜的小朋友编号为多少?
  16. 输入格式
  17.   输入一行,包括两个整数nk,意义如题目所述。
  18. 输出格式
  19.   输出一行,包含一个整数,表示获胜的小朋友编号。
  20. 样例输入
  21. 5 2
  22. 样例输出
  23. 3
  24. 样例输入
  25. 7 3
  26. 样例输出
  27. 4
  28. 数据规模和约定
  29.   对于所有评测用例,1 n 10001 k 9

思路:使用队列实现。

  1. import java.util.ArrayDeque;
  2. import java.util.Queue;
  3. import java.util.Scanner;
  4.  
  5. public class Main_Game {
  6. public static void main(String[] args) {
  7. Scanner sc = new Scanner(System.in);
  8. int count = sc.nextInt();// 小朋友个数
  9. int num = sc.nextInt();// 需要数的数
  10. int currnum = 1;
  11. Queue<Integer> queue = new ArrayDeque<Integer>();// 小朋友组成的队列
  12. for (int i = 1; i <= count; i++) {
  13. queue.add(i);
  14. }
  15. while (queue.size() > 1) {
  16. int top = queue.element();//队首元素
  17. queue.remove();//删除队首元素
  18. if(currnum % num !=0 && currnum %10 != num){
  19. //如果不是num的倍数并且不包含num
  20. queue.add(top);//将元素加入队尾
  21. }
  22. currnum ++;
  23. }
  24. System.out.println(queue.element());
  25. }
  26. }

链表实现:

  1. import java.util.LinkedList;
  2. import java.util.List;
  3. import java.util.Scanner;
  4.  
  5. public class CountGame {
  6. public static void main(String[] args) {
  7. Scanner sc = new Scanner(System.in);
  8. int count = sc.nextInt();// 小朋友个数
  9. int num = sc.nextInt();// 需要数的数
  10. int currnum = 0;
  11. int currid = 0;
  12. List<Person> pers = new LinkedList<Person>();
  13. for(int i = 0; i < count; i++){
  14. Person pes = new Person(i+1);
  15. pers.add(pes);
  16. }
  17. while (pers.size() > 1) {
  18. currnum ++;
  19. if (currnum % num != 0 && currnum%10!=num) {
  20. // 数到不是num的倍数也不包含num的数
  21. pers.get(currid).setNumber(currnum);
  22. }else{
  23. pers.remove(currid);
  24. if(currid >= pers.size()){
  25. currid = 0;
  26. }
  27. continue;
  28. }
  29. if(currid < pers.size()-1){
  30. currid++;
  31. }else{
  32. currid = 0;
  33. }
  34. }
  35. System.out.println(pers.get(0).getId());
  36. }
  37. }
  38. class Person {
  39. private int id;
  40. private int number;
  41.  
  42. public Person(){}
  43.  
  44. public Person(int id){
  45. this.id = id;
  46. }
  47.  
  48. public Person(int id,int number){
  49. this.id = id;
  50. this.number = number;
  51. }
  52.  
  53. public int getId() {
  54. return this.id;
  55. }
  56.  
  57. public void setId(int id) {
  58. this.id = id;
  59. }
  60.  
  61. public int getNumber() {
  62. return number;
  63. }
  64.  
  65. public void setNumber(int number) {
  66. this.number = number;
  67. }
  68.  
  69. }

CCF认证201712-2游戏的更多相关文章

  1. CCF认证历年试题

    CCF认证历年试题 不加索引整理会死星人orz 第一题: CCF201712-1 最小差值(100分) CCF201709-1 打酱油(100分) CCF201703-1 分蛋糕(100分) CCF2 ...

  2. 小明种苹果(续)第十七次CCF认证

    小明种苹果(续)第十七次CCF认证 题目 原题链接 ](http://118.190.20.162/view.page?gpid=T93) 很高心,在现在CCF CSP可以下载自己当时的答卷了,也就是 ...

  3. CCF认证(1)

    #include <iostream> #include <windows.h> using namespace std; typedef struct letter{ int ...

  4. CCF 认证4

    题意:求强联通分量 Tarjan算法 #include<iostream> #include<stdio.h> #include<stdlib.h> #includ ...

  5. CCF 认证

    题意:字符串替换 string+map的应用 #include<iostream> #include<stdio.h> #include<stdlib.h> #in ...

  6. CCF认证考试——折点计数

    描述:简单题 #include<iostream> using namespace std; int main() { ], n, count = ; cin >> n; ; ...

  7. CCF认证之——相反数

    这道题目非常简单! #include<iostream> using namespace std; int main() { ],n,count=; cin >> n; ; i ...

  8. ccf认证 201709-4 通信网络 java实现

    试题编号:                                                               201709-4 试题名称: 通信网络 时间限制: 1.0s 内 ...

  9. ccf认证模拟题之三---最大的矩形

    问题描述 在横轴上放了n个相邻的矩形,每个矩形的宽度是1,而第i(1 ≤ i ≤ n)个矩形的高度是hi.这n个矩形构成了一个直方图.例如,下图中六个矩形的高度就分别是3, 1, 6, 5, 2, 3 ...

随机推荐

  1. 阿里云短信服务Java版

    短信服务管理平台 https://dysms.console.aliyun.com/dysms.htm java短信发送API    https://help.aliyun.com/document_ ...

  2. Hive安装与应用过程

    1.  参考说明 参考文档: https://cwiki.apache.org/confluence/display/Hive/GettingStarted 2.  安装环境说明 2.1.  环境说明 ...

  3. 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 ...

  4. arcgis JavaScript 加载 mapbox地图

    mapbox 地图现在是越来越好看了, 随便试 /** * Created by Administrator on 2018/5/15 0015. */ import * as esriLoader ...

  5. 调用Android中的软键盘

    我们在Android提供的EditText中单击的时候,会自动的弹 出软键盘,其实对于软键盘的控制我们可以通过InputMethodManager这个类来实现.我们需要控制软键盘的方式就是两种一个是像 ...

  6. linux 文件操作命令 touch、cat、more、less、head、tail

    touch /bin/touch 创建空文件 linux 创建文件可以使用特殊符号,/除外 touch test test1 创建了两个文件touch "test test1" 创 ...

  7. WiFi 干扰器,有时间可以去试试呦!

    转自社区: 0X01 引言 想不想搞个WIFI干扰器?网上搜集了一下资料,发现用esp8266可以实现简单的干扰功能,包括断网.复制.欺骗等等.刚好手上有块Tpyboard V202(30元),也是e ...

  8. ssm单项目整合

    目录 前言 创建maven项目 添加依赖 配置文件 总览 jdbc配置 mybatis配置 dao层配置 service层配置 事务配置 controller配置 web.xml 使用 前言 spri ...

  9. 记开发个人图书收藏清单小程序开发(十)DB开发——新增图书信息

    昨晚完成了Web端新增图书信息的功能,现在就差DB的具体实现了. 因为我把Book相关的信息拆分的比较多,所以更新有点小麻烦. 首先,我需要创建一个Book Type的Matter: 然后,将图片路径 ...

  10. Tomcat6.0下的jsp、servlet和javabean的配置

    第一步:下载jdk和tomcat: 第二步:安装和配置你的jdk和tomcat:执行jdk和tomcat的安装程序,然后设置按照路径进行安装即可.1.安装jdk以后,需要配置一下环境变量,在我的电脑- ...