一道卡了我很久的水题
引入 约瑟夫环
题目背景
约瑟夫是一个无聊的人!!!
题目描述
n个人(n<=100)围成一圈,从第一个人开始报数,数到m的人出列,再由下一个人重新从1开始报数,数到m的人再出圈,……依次类推,直到所有的人都出圈,请输出依次出圈人的编号.
输入输出格式
输入格式:
n m
输出格式:
出圈的编号
输入样例#1:
10 3
输出样例#1:
3 6 9 2 7 1 8 5 10 4
简单的模拟
#include <bits/stdc++.h>
using namespace std;
inline int read()
{
int s = 0, w = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-') w = -1;
ch = getchar();
}
while(ch >= '0' && ch <= '9')
{
s = (s << 1) + (s << 3) + ch - '0';
ch = getchar();
}
return s * w;
}
#define MAXX 110
int n, m;
int a[MAXX];
int ans = 0;
int all;
int main()
{
memset(a, 0, sizeof(a));
n = read();
m = read();
all = n;
for(int i = 1; i <= n; ++i)
{
if(a[i] != 1) ans++;
if(ans == m)
{
a[i] = 1;
printf("%d ", i);
all--;
ans = 0;
}
if(i == n && all >= 1)
{
i = 0;
}
}
return 0;
}
猴子选大王
还是简单的模拟(只不过比约瑟夫复杂了一点)
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int s = 0, w = 1;
char ch = getchar();
while(!isdigit(ch)) { if(ch == '-') w = -1; ch = getchar(); }
while(isdigit(ch)) { s = (s << 1) + (s << 3) + ch - '0'; ch = getchar();}
return s * w;
}
#define MAXX 1100
int n, x;
int a[MAXX];
int k, kk;
int main() {
k = 1;
n = read(); x = n;
memset(a, 0, sizeof(a));
int ans = 1;
for(int i = 1; i <= n; ++i) {
if(ans == 4) ans = 1;
a[i] = ans;
++ans;
}
while(x > 2) {
for(int i = 1; i <= n; ++i) {
if(a[i]) {
if(a[i] == 3) {
a[i] = 0;
x--;
}
}
}
if(k) {
k = 0; kk = 1;
int ans = 1;
for(int i = n; i >= 1; --i) {
if(ans == 4) ans = 1;
if(a[i]) {
a[i] = ans;
++ans;
}
}
}
else {
if(kk) {
k = 1; kk = 0;
int ans = 1;
for(int i = 1; i <= n; ++i) {
if(ans == 4) ans = 1;
if(a[i]) {
a[i] = ans;
ans++;
}
}
}
}
}
for(int i = 1; i <= n; ++i) {
if(a[i] == 1) printf("%d", i);
}
return 0;
}