当前位置:网站首页>Joseph problem C language
Joseph problem C language
2022-06-30 06:36:00 【Oraer_】
The time limit : 1 Sec Memory limit : 128 MB
Title Description
Joseph's question : Yes n Only monkey , Circle clockwise to choose the king ( Number from 1 To n), from The first 1 Number Start counting , Count to m, Count to m The monkey out of the circle , The rest of the monkeys went on from 1 Start counting . That's it , Until there's only one monkey left in the circle , This monkey is the monkey king , Programming for input n,m after , Output the number of the last Monkey King .
Input
Each line is two integers separated by spaces , The first is n, The second is m ( 0 < m,n <=300). The last line is :
0 0
Output
For each row of input data ( Except for the last line ), The output data is also a line , The number of the last Monkey King
The sample input
6 2
12 4
8 3
0 0
Sample output
5
1
7
Ideas :
The idea is very simple. Use an array to record everyone's elimination status , Initialize all to 0, Obsolete assigned as 1,i=(i+1)%n Form a ring .
#include<stdio.h>
int main()
{
int m,n;
while(1)
{
int a[305]={
0};
scanf("%d%d",&n,&m);
if(n == 0 && m == 0)
break;
int t=n-1,i=-1;
int count=0;
while(t--)
{
while(count != m)
{
i=(i+1)%n; // The problem is from 1 Start , We are from 0 At the beginning
if(a[i] == 0)
count++;
}
a[i]=1;
count=0;
}
for( i=0; i<n; i++)
if(a[i] == 0)
break;
printf("%d\n",i+1); // Be sure to add one
}
return 0;
}
Time complexity bit O(n*m)
边栏推荐
- Introduction to neural networks
- Rhcsa day 1
- Image processing 7- image enhancement
- IO stream (file class introduction)
- ES6 array
- Practice summary of Prometheus project in amu Laboratory
- As function memo
- To: k210 realizes face recognition (code interpretation attached)
- New project folder based on PIO plug-in in vscode -- Interpretation
- 1.7 - CPU的性能指标
猜你喜欢
随机推荐
Gazebo installation, uninstall and upgrade
1.7 - CPU的性能指标
Wechat applet mall project
Win10 /11 开热点无法上网问题
PHP knowledge points
【微信小程序:单选、多选样式,背景色,圆角】
C language final experiment report (student achievement management system) source code
ROS multi machine
Basic use of markdown
1.9 - 存储器的分类
File transfer protocol, FTP file sharing server
MySQL中的InnoDB引擎
Use and principle of completionservice (source code analysis)
ini解析学习文档
01. 正则表达式概述
1.2(补充)
Four ways to create multithreads
Idea add database
Verilog中case,casez,casex语句的用法
Completabilefuture: from understanding to mastering, here are all you want to know









