首页 > 学院 > 开发设计 > 正文

1188_约瑟夫环

2019-11-10 22:11:00
字体:
来源:转载
供稿:网友
// 1188_约瑟夫环.cpp : 定义控制台应用程序的入口点。//题目1188:约瑟夫环//时间限制:1 秒内存限制:32 兆特殊判题:否提交:2141解决:912//题目描述://N个人围成一圈顺序编号,从1号开始按1、2、3......顺序报数,报p者退出圈外,其余的人再从1、2、3开始报数,报p的人再退出圈外,以此类推。//请按退出顺序输出每个退出人的原序号。//输入://包括一个整数N(1<=N<=3000)及一个整数p。//输出://测试数据可能有多组,对于每一组数据,//按退出顺序输出每个退出人的原序号。//样例输入://7 3//样例输出://3 6 2 7 5 1 4//来源://2003-2005年华中科技大学计算机研究生机试真题#include "stdafx.h"#include "stdio.h"#include "iostream"#include "string"using namespace std;int a[3010];int main(){ int n,p; while(cin>>n>>p){ for(int i = 1;i<=n;i++){ a[i] = i; } int remain = n; int count = 0; int i = 1; while(1){ if(i>n) i = 1; if(a[i]!=-1){ count++; if(count==p){ remain--; if(remain==0){ cout<<a[i]<<endl; break; } else cout<<a[i]<<" "; a[i] = -1; count = 0; } } i++; } } return 0;}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表