当前位置:网站首页>OJ 1018 counting game
OJ 1018 counting game
2022-07-28 06:39:00 【JETECHO】
describe
n A circle of individuals ( The number is 1 - n), From 1 Individual starts counting , To report for duty k The people of , The people in the back start from 1 Start counting . Ask the number of the last remaining person .
for example :n = 3,k = 2.2 No. first out , And then there was 1 Number , The last thing left is 3 Number .
Input
Input is a single set of test data .
Input 2 Number n and k, Express n personal , Count to k List out .(2 <= n, k <= 200)
Output
Output an integer to represent the number of the last person left .
sample input 1
10 3
sample output 1
4
The topic is a problem of yasuf ring , The title explains In the first n No. is about to delete a data , And then start again from the beginning , Cycle all the time , Until the end , That is to say, they are deleted step by step , If there is only one good analysis, the first one is the serial number 1, If it's two, it's in 1,2 Count off and know that a person has been kicked out , Such as n=4, So that is 1,2,1,2, It's counting 1,2,3,4 Last 2 Kicked out, so the serial number is 1 , Three is the same , Three numbers, that is 1,2,3,1//2,3,2,3 Last , With recursion, you will find a rule , Hypothetical use f It means to find the serial number , Yes k personal , newspaper n Then kick out , In the example n=4, that f(1)=0,f(2)=1,f(3)=2……-->f(2)=(f(1)+n)%2=0+1=1,f(3)=(f(2)+n)%3=1+1=2……f(k)=(f(k-1)+n)%k. That's the formula ,f(k)=(f(k-1)+n)%k, If k=1 Then it must be 0, So we can use 1 Special to build functions ,1 Pushable 2,2 Pushable 3, By analogy, we get the final answer .
#include <iostream>
using namespace std;
int n,k;
int f()
{
int s=0;
for(int i=2;i<=k;i++)
s=(s+n)%i;
return s+1;
}
int main()
{
while(cin>>k>>n)
{
cout<<f()<<endl;
}
return 0;
}
边栏推荐
- 2022-06-07 VI. log implementation
- 气传导蓝牙耳机哪个好、气传导蓝牙耳机排行榜
- OJ 1451 digital games
- What are the open earphones? Four types of air conduction earphones with excellent sound quality are recommended
- AQS之CyclicBarrier源码解析
- [c语言]简易通讯录的实现
- Feignclient @requestmapping parameter setting and simple method setting of request header
- Everything you don't know about time complexity is here
- Leetcode 刷题日记 剑指 Offer II 047. 二叉树剪枝
- 图形管线基础(一)
猜你喜欢

气传导蓝牙耳机什么牌子好、气传导耳机最好的品牌推荐

万字归纳总结并实现各大常用排序及性能对比

QT batch operation control and set signal slot

Development of Quantitative Trading Robot System

2022年七夕送女朋友什么礼物好?实用且好看的礼物推荐

用c语言实现三子棋小游戏

What's a gift for girls on Chinese Valentine's day? Selfie online and thoughtful gift recommendation

2022-06-07 responsebodyadvice caused the spring frame problem in swagger

藏宝计划TPC系统开发Dapp搭建

水瓶效果制作
随机推荐
Leetcode skimming diary sword finger offer II 050. sum of downward path nodes
QT painting event - set background picture
水渲染示例
如何模拟实现strcpy库函数
AQS之ReentrantLock源码解析
What's a gift for girls on Chinese Valentine's day? Selfie online and thoughtful gift recommendation
下雨场景效果(一)
AQS之countDownLatch源码分析
Problem solving for ACM freshmen in Jiangzhong on October 26
Execjs call
Everything you don't know about time complexity is here
Several methods of QT setting loading interface
AQS之CyclicBarrier源码解析
[c语言]简易通讯录的实现
[basic knowledge of binary tree]
浮点型数据在内存中如何存储
网络通信及TCP/IP协议
OJ 1253 点菜问题
OJ 1505 保险丝
js 变量等于0也等也' '问题