当前位置:网站首页>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)
边栏推荐
猜你喜欢
随机推荐
Unable to read file for extraction: gdx64. dll
1.9 - Cache
Detailed description of methods in the interface
File transfer protocol, FTP file sharing server
银河麒麟初体验
File Transfer Protocol,FTP文件共享服务器
Common address collection
My experience in functional testing for so many years
As function memo
When to use redis
Win10 /11 开热点无法上网问题
DXP copper laying settings
神经网络入门
Learn fpga---ram IP core and key parameters from the bottom structure
Set in set (III)
Hao looking for a job
Simple example of class template
Improve simulation speed during ROS and Px4 joint simulation
Redux source code implementation
Rhcsa day 1









