小程序游戏SDK如何推动游戏开发的快速创新与变革
846
2022-11-17
简单的Josephus环(课本例题复习)
// ConsoleApplication5.cpp : 此文件包含 “main” 函数。程序执行将在此处开始并结束。 //链表 //Josephus环问题 #include “pch.h” #include using namespace std; struct Jonse { int code; Jonse *next; }; Jonse * create(int); void showList(Jonse *); void OutList(Jonse *, int,int); int main() { Jonse *head; int num, val, beg; cout << "\nplease input the number of the list: " << endl; cin >> num; head = create(num); cout << “开始位置” << endl; cin >> beg; cout << “间隔” << endl; cin >> val; showList(head); OutList(head, beg, val); } Jonse * create(int m) { Jonse *h, *p; h = new Jonse; p = h; for (int i = 1; i <= m; i++) { p->code = i; if (i < m) { p->next = new Jonse; p = p->next; } } p->next = h; p = h; return h; } void showList(Jonse * h) { Jonse *p; p = h; do { cout << p->code << endl; p = p->next; } while (p != h); } void OutList(Jonse * h, int i, int d) {//i 头,d间隔 Jonse *p, *q; int k; p = h; //q是p的前驱指针,指向最后建立的结点 //当k=1时。q还是p的前驱,下面执行一次 for (q = h; q->next != h; q = q->next); //当k>1时 for (k = 1; k < i; k++) { q = p; p = p->next; } //begin while (p != p->next) { for (k = 1; k < d; k++) { q = p; p = p->next; } cout << p->code << endl; q->next = p->next; delete p; p = NULL; p = q->next; } cout << “最后结点数字” << p->code << endl; delete p; p = NULL; }
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
发表评论
暂时没有评论,来抢沙发吧~