博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
猴子选大王
阅读量:7055 次
发布时间:2019-06-28

本文共 1228 字,大约阅读时间需要 4 分钟。

Description

猴子选大王,有N只猴子,从1~N进行编号。它们按照编号的顺时针方向,排成一个圆圈,然后从第一只猴子开始报数。第一只猴子报1,以后每只猴子报的数字都是它前面猴子所报数字加1。如果一只猴子报的数字是M,则该猴子出列,下一只猴子重新从1开始报数。剩下的猴子继续排成一个圆圈报数,直到全部的猴子都出列为止。最后一个出列的猴子胜出。

 

Input

The first line is an integer t, indicating the number of test cases. Then there are t lines and each line contains two positive integer N(0<N<=1000) and M(0<M<=10000).

Output

For each test case, print out the number of the Monkey King.

Sample Input
 Copy sample input to clipboard
25 24 3
Sample Output
31
#include
using namespace std;struct Node{ Node* next; int data;};Node* Create(int n){ Node* head = new Node; Node*p, *pre; head->next = NULL; head->data = 1; pre = head; for (int i = 2; i <= n; i++) { p = new Node; p->data = i; p->next = NULL; pre->next = p; pre = p; } pre->next = head; return head;}void ChooseKing(Node* h, int M){ int count; while (h->next != h) { count = 1; while (count
next; count++; } Node*q = h->next; h->next = h->next->next; h = h->next; delete q; } cout << h->data << endl; delete h;}int main(){ int m; cin >> m; while (m-->0) { int n, M; cin >> n >> M; Node*h = Create(n); ChooseKing(h, M); } return 0;}

转载于:https://www.cnblogs.com/KennyRom/p/5910439.html

你可能感兴趣的文章
接口JSon字符串格式
查看>>
十大流行Linux发行版
查看>>
微信公众平台消息接口开发之微信浏览器HTTP_USER_AGENT判断
查看>>
Android自定义ScrollView实现一键置顶功能
查看>>
samba配置
查看>>
php -- realpath($path) 函数
查看>>
Web开发之Goahead
查看>>
crm工作机会实体
查看>>
[分享] 【旧文新读】少一分火气,多一份和气
查看>>
STM32探秘 之FSMC
查看>>
一脸懵逼学习基于CentOs的Hadoop集群安装与配置(三台机器跑集群)
查看>>
dfs常见的配置文件中的value与description(重要)
查看>>
【spring Boot】spring boot1.5以上版本@ConfigurationProperties取消location注解后的替代方案...
查看>>
条件编译
查看>>
【Spring】构建Spring Web应用
查看>>
SharePoint Online 创建列表库
查看>>
BZOJ4912 : [Sdoi2017]天才黑客
查看>>
Hibernate 中配置属性详解(hibernate.properties)
查看>>
[Django学习]上传图片
查看>>
Python实现屏幕截图的两种方式
查看>>