#include<iostream>
#include<string.h>
#include<cstdio>
#include <sstream>
using namespace std;
template <class T>
class joseph
{
struct node
{
T data;
node * next;
node():next(NULL) {}
node(T d):data(d),next(NULL) {}
};
private:
node * head;
node * cur;
node * pre; public:
joseph(T d);
int len = 0;
void setValue(T d)
{
node * tem = new node(d);
cur ->next = tem;
tem -> next = head;
cur = cur -> next;
pre = pre -> next;
len++;
}
void out()
{
pre -> next = cur -> next;
cout<<cur -> data<<" ";
delete cur;
cur = pre -> next;
len--;
}
void nextmove()
{
cur = cur -> next;
pre = pre -> next;
}
void init()
{
cur = cur -> next;
pre = pre -> next;
} };
//模板函数:将string类型变量转换为常用的数值类型(此方法具有普遍适用性)
template <class Type>
Type stringToNum(const string& str)
{
istringstream iss(str);
Type num;
iss >> num;
return num;
} template <class T>
joseph<T> :: joseph(T d)
{
head = new node(d);
node * fz = new node();
cur = head;
pre = fz;
pre -> next = head;
}
int main()
{
string z,x;
cout<<"please input n"<<endl;
cout<<"please input m"<<endl;
while(cin>>z>>x)
{
int a = 1 ;//为第一个节点赋值而创建的
int j = 0 ;//j为n的输入判断因子,当j=1时说明输入的n不是int型
int k = 0 ;//k为m的输入判断因子,当j=1时说明输入的n不是int型
joseph<int> dusk(a);//创建第一个节点,并调用构造函数把第一个节点赋值为1
dusk.len++;//链表长度加一
int n , m ;//n为人数,m为密码
int flag = 0;//是否继续
for(int i = 0 ; i < z.length() ; i++)//判断n是不是int型
{
if(z[i]<'0'||z[i]>'9')
{
cout<<"n input error"<<endl;
j = 1;
flag = 1;
break;
}
}
n = stringToNum<int>(z);//调用函数把string型的n转换成int型
for(int i = 0 ; i < x.length() ; i++)//判断m是不是int型
{
if(x[i]<'0'||x[i]>'9')
{
cout<<"m input error"<<endl;
k=1;
flag = 1;
break;
}
}
if(flag)
{
cout<<"please input n"<<endl;
cout<<"please input m"<<endl;
continue;
}
m = stringToNum<int>(x);//调用函数把string型的m转换成int型
if(n == 1)
{
cout << 1 << endl;
cout<<"please input n"<<endl;
cout<<"please input m"<<endl;
continue;
}
if(k==1||j==1)break;//判断因子有一个等于1说明:n,m有一个输入的不是int型,结束循环 for(int i = 2 ; i <= n ; i++)//初始化赋值
{
dusk.setValue(i);
}
dusk.init();//把cur指针指向head,把pre的next指向cur
while(dusk.len!=0)//长度不为0时循环
{
for(int i = 1 ; i < m ; i++)//移动
{
dusk.nextmove();
}
dusk.out();
}
cout<<endl;
cout<<"please input n"<<endl;
cout<<"please input m"<<endl;
} }

  

最新文章

  1. Python列表和元组
  2. Android中对内存和外存的读写
  3. HDU 3727 Jewel 可持久化线段树
  4. OS X 禁止Android File Transfer自动启动
  5. PHP读书笔记(6)- 数组
  6. 【80端口占用】win7下80端口被(Pid=4)占用的解决方法
  7. 两台linux机器文件传输之scp
  8. PDF在线阅读 FlexPaper 惰性加载 ;
  9. Android 之xml解析
  10. [原创软件]Maya语言切换工具
  11. 关于.net导出数据到excel/word【占位符替换】
  12. JAVA进阶9
  13. Oracle创建表空间、用户管理、角色管理
  14. 使用spring提供的ReflectionUtils简化项目中反射代码的复杂性
  15. 带你从零学ReactNative开发跨平台App开发(七)
  16. MySQL: OPTIMIZE TABLE: Table does not support optimize, doing recreate + analyze instead
  17. [整理]内存重叠之memcpy、memmove
  18. c++ 类的构造顺序
  19. android:Activity四种启动模式简单介绍
  20. MySQL 最基本的SQL语法/语句

热门文章

  1. clientdataset 读取excel 如果excel 文件不存在的时候 相应的gird 会不显示数据, 鼠标掠过 gird 格子 才会显示数据。 这是一个bug 哈哈
  2. C#之委托(二)
  3. Altium Designer chapter8总结
  4. 排序算法七:基数排序(Radix sort)
  5. oracle--登陆用户机制
  6. repr_str方法
  7. idea 社区版本创建javaweb项目 使用tomcat
  8. [Python3 填坑] 016 对 __getattr__ 和 __setattr__ 举例
  9. Java数据结构之稀疏数组(Sparse Array)
  10. Latex--入门系列三