//问题是约瑟夫环,报到对应的数字的人出圈并且输出该人的序号
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std;
int a[100];
int main() {
int n, m;
int j = 0;
int t = 0;
cin >> n >> m;
memset(a, false, sizeof(a));//初始false都在队里
for (int i = 1; i <= n; i++) {
if (!a[i]) {//因为在圈内是0,所以这里用的!
j++;
if (j == m) {
a[i] = true;//出列
cout << i << endl;
t++;//计算已有多少人出圈
j = 0;
if (t == n)//出圈人数到达n,结束程序
return 0;
}
}
if (i == n )//当到最后一个人,回到队伍开头
i = 1;
}
return 0;
}
我输入的8 3,运行结果是3
6
2
7
5
8
4,这明显不对,我的1跑哪了呢??????
犯了低级迷糊,i会在每次for循环结束之后自增,所以设1的话,结束时实际定位到第二个人,哈哈哈哈哈哈哈哈哈哈哈哈豆沙了