陕西师范大学第七届程序设计竞赛网络同步赛D ZQ的睡前故事【约瑟夫环1-N数到第k个出队,输出出队顺序/ STL模拟】

链接:https://www.nowcoder.com/acm/contest/121/D
来源:牛客网

题目描述

    ZQ是一个拥有n女朋友的万人迷,她的每一个女朋友每天晚上都会挨个给他打电话,要他讲了睡前故事才能睡觉。可是,每次他的女朋友都会挑他在吃鸡的时候打电话,ZQ总是因为挂机被舍友赶出宿舍,于是,ZQ告诉他的女朋友们,别打电话了,他会主动打过去给他们讲故事,再打电话就分手!

    于是,ZQ把他的女朋友名字写在纸上,画成一圈,顺时针编号为1~n,然后从1开始顺时针数。在每一次数数中,ZQ数k个就停下来,然后给选中的女朋友打电话讲故事。 

    现在需要你按顺序告诉我们他给女朋友打电话的顺序

输入描述:

先输入一个t,然后t组数据,每行包含两个数字n,k,n<20,k>0

输出描述:

按顺序输出每轮被选中的女朋友的编号。
示例1

输入

3
10 3
5 2
11 4

输出

3 6 9 2 7 1 8 5 10 4
2 4 1 5 3
4 8 1 6 11 7 3 2 5 10 9

【代码】:
#include<bits/stdc++.h>
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,n,x) for(int i=(x); i<(n); i++)
#define in freopen("in.in","r",stdin)
#define out freopen("out.out","w",stdout)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxn = 1e3 + 20;
const int maxm = 1e6 + 10;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int dx[] = {-1,1,0,0,1,1,-1,-1};
const int dy[] = {0,0,1,-1,1,-1,1,-1};
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int n,k;
int a[maxm],b[maxm],c[maxm];
int main()
{
   int T;
   cin>>T;
   while(T--)
   {
        cin >> n >> k;
        queue<int> q;
        for(int i=1; i<=n; i++) q.push(i);

        int cnt = 1, flag = 1;
        while(!q.empty())
        {
            int t = q.front(); //取队头
            q.pop();
            if(cnt % k == 0){ //数到第k个了
                if(flag) printf("%d", t), flag = 0; //输出格式
                else printf(" %d", t);
            }
            else q.push(t);  //没数到就压入队列
            cnt++;
        }
        printf("
");
   }
}
原文地址:https://www.cnblogs.com/Roni-i/p/9037828.html