数组实现约瑟夫环
2016-05-11 22:18
211 查看
#include<iostream> using namespace std; void cover_num(int *arr, int pos, int n)//覆盖掉pos位置的数 { for (int i = pos; i < n-1; i++) { arr[i] = arr[i + 1]; } } void out_circle(int *arr, int n) { int num = 1; int index = 0; while (n > 1) { int count = 2;//报三次数相当于跳两次 while (count--) { if (n == index){ index = 0; }//到达尾部时再回到0位置 index++; if (n == index){ index = 0;} } cout <<"第"<<num<<"个出圈的数是:"<< arr[index] << endl; cover_num(arr, index, n); n--; num++; } } void main() { int array[100]; for (int i = 0; i < 100; i++) { array[i] = i + 1; } cout << "\n共100人报数为三的人出圈\n"; out_circle(array, 100); cout <<"\n最后留下的人是:" <<array[0] << endl; system("pause"); }
相关文章推荐
- 杨辉三角
- 特殊回文数
- 魔方阵
- 蛇形填数
- 倒排句子的单词
- Andriod Checkbox
- 《python灰帽子》学习笔记:写一个windos 调试器(一)
- ORACLE报错
- 使用js实现上移、下移、置顶、置底功能及源码案例
- 建造者设计模式
- leetcode-23-Merge k Sorted Lists
- idea 将工程项目打包成war
- hdu——1010Tempter of the Bone
- C++回车换行时停止读入
- 改进的经度坐标校正鱼眼图像算法_code
- iOS之XMPP环境搭建
- 如何用js得到当前页面的url信息方法(JS获取当前网址信息)
- ACL权限的学习
- r语言常用函数apply及subset函数
- 模板方法设计模式扩展(Hook Method)