欢迎使用本站,预祝练习时长两年半的选手们到成功! [本模块信息来自tem/def/head]

tey zzj 约瑟夫plus版

时间:2024-05-18 16:53 作者:admin 点击:
输入:n,m,k,人数,死亡数字,存活数量 输出:将存活的人以编号从小到大输出 输入:60 17 5 输出:7 9 10 45 58 #include iostream #include queue #include algorithm using namespace std; queueint q;//创建一个

输入:n,m,k,人数,死亡数字,存活数量

输出:将存活的人以编号从小到大输出

输入:60 17 5

输出:7 9 10 45 58

#include <iostream>

#include <queue>

#include <algorithm>

using namespace std;

queue<int> q;//创建一个约瑟夫队列

int main(){

   int N,M;

   cin>>N>>M;

   for(int i=1;i<=N;i++) q.push(i);

   int c=1;

   while(q.size()!=k){

       if(c==M){

           cout<<q.front()<<" ";

           q.pop();

           c=1;

       }

       else{

           q.push(q.front());

           q.pop();

           c++;

       }

   }

   int a[1000]={},j=0;

   while(!q.empty()){

       a[j++]=q.front();

       q.pop();

   }

   sort(a,a+j);

   for(int i=0;i<j;i++) cout<<a[i]<<" ";

   return 0;

}


//单词: queue 队列

(责任编辑:admin)
    顶一下
    (0)
    0%
    踩一下
    (0)
    0%