约瑟夫问题-函数功能分解
2015-03-21 20:25
288 查看
#include<cstdlib> #include<iostream> using namespace std; struct Node { int num; Node *next; }; Node *Lcreat(int num) { int cnt = 0; Node *h = NULL; Node *pr = new Node; h = pr; h->num = num; while(--num) { Node *p = new Node; p->num = num; p->next = h; h = p; } pr->next = h; return pr; } int findJos(int num, int span) { Node *p = Lcreat(num); for(int i = 1; i < num; ++i) { for(int j = 1; j < span; ++j) p = p->next; Node *tp = p->next; p->next = tp->next; cout << tp->num << ' '; delete tp; } int t = p->num; cout << p->num << endl; delete p; return t; } int main(void) { int m, n; while(cin >> m >> n) cout << "The last one is No." << findJos(m, n) << endl; system("pause"); return 0; }建立链表->模拟操作->输出结果.
相关文章推荐
- 关于android开发时,eclipse的函数自动提示功能很卡的问题解决方法
- HDOJ-1106 分解字符串、转化及排序问题总结[sscanf()函数+strtok()函数+atoi()函数]
- 通过调用顺序表函数来求解“约瑟夫问题”
- <<通用的输入输出---------cout的扩展>> 引出的问题 ——扩展库函数功能
- 实现倒计时功能--函数的局部变量问题
- lib函数添加功能但是函数名和参数不变,重新封装成DLL有关问题
- C++ seekp 函数文件流跳转功能产生数据覆盖问题解决
- Scheme之功能分解和封装内部函数/块结构
- 面向过程就是分析出解决问题所需要的步骤,然后用函数把这些步骤一步一步实现,使用的时候一个一个依次调用就可以了;面向对象是把构成问题事务分解成各个对象,建立对象的目的不是为了完成一个步骤,而是为了描叙某个事物在整个解决问题的步骤中的行为(转)
- 以上的文章是我解决这样一个问题时候的大致的思路: 把Matlab读取二进制文件的函数变成VBA读取二进制文件,实现同样的功能
- 关于SetMargins 使用的问题,在CRichEdit中实现与CEdit中一样的功能的函数。
- 按如下函数原型用函数编程解决如下的日期转换问题(要求考虑闰年的问题): 输入某年某月某日,计算并输出它是这一年的第几天。 /* 函数功能: 对给定的某年某月某日,计算它是这一年的第几天 函数参数:
- 关于函数返回的局部数组的问题
- 提几个关于博客园功能上的问题和建议
- [Asp Friend]问题收集与可能的功能扩充
- oracle里是否有类似于c语言的split()函数的功能???
- 约瑟夫问题
- 一个JAVASCRIPT实用函数,完成合并表格中列中具有相同内容的列单元格的功能
- SQL Server 2000之日志传送功能 - 问题解决
- 关于rownum排序分页会比分析函数排序快的问题。。。