画出该程序的流程图#include<iostream> #include<iomanip> #include"kids.h" #include"countingchildren.h" #include"print.h" using namespace std; Kid* pfirst = 0; Kid* pcurrent = 0; Kid* pivot = 0; void main() { int numberofboys, m; cout << "请输入参与约瑟夫的小孩人数"; cin >> numberofboys; cout << endl; cout << "请输出约瑟夫问题的间隔数:"; cin >> m; cout << endl; pfirst = new Kid; pfirst->code = 1; pfirst->pnext = pfirst; pcurrent = pfirst; for (int i = 1; i < numberofboys; i++) { pivot = pcurrent; pcurrent = new Kid; pcurrent->code = i + 1; cout << "请输入第" << i << "个小孩名字"; cin >> pcurrent->name; pivot->pnext = pcurrent; } pcurrent->pnext = pfirst; cout << setw(4) << pfirst->code;//遍历输出 pcurrent = pfirst->pnext; while (pcurrent != pfirst) { cout << setw(4) << pcurrent->code; pcurrent = pcurrent->pnext; } cout << endl; countchildren(pfirst, m); printWinner(pcurrent); } //countchildren.h void countchildren(Kid* pfirst, int m); //countchildren.cpp #include<iostream> #include<iomanip> #include"kids.h" #include"leave.h" #include"print.h" using namespace std; void countchildren(Kid* pfirst, int m) { Kid* pcurrent = 0; Kid* pivot = 0; pcurrent = pfirst; int j; while (pcurrent->pnext != pcurrent) { j = m; do { pivot = pcurrent; pcurrent = pcurrent->pnext; j--; } while (j > 1); printLoser(pcurrent); leave(pivot, pcurrent); } } //print.h #pragma once #include<iostream> #include<iomanip> #include"kids.h" #include"countingchildren.h" using namespace std; void printWinner(Kid* winner); void printLoser(Kid* loser); //print.cpp #include<iostream> #include<iomanip> #include"kids.h" #include"countingchildren.h" using namespace std; #include "print.h" void printWinner(Kid* winner) { cout << "最后胜利的人:" << endl; cout << "其编号:" << setw(4) << winner->code << endl; cout << "名字:" << winner->name << endl; } void printLoser(Kid* loser) { cout << "这轮失败的人:" << endl; cout << "其编号:" << setw(4) << loser->code << endl; cout << "名字:" << loser->name << endl; } //Leave.h #pragma once #include<iostream> #include<iomanip> #include"kids.h" #include"countingchildren.h" using namespace std; void leave(Kid* pivot, Kid* pcurrent ); //leave.cpp #include<iostream> #include<iomanip> #include"kids.h" #include"countingchildren.h" using namespace std; void leave(Kid* pivot, Kid* pcurrent) { pivot->pnext = pcurrent->pnext; delete pcurrent; pcurrent = pivot->pnext; }
时间: 2024-03-07 12:51:19 浏览: 83
vc++2008编译不了#include<iostream.h>头文件
4星 · 用户满意度95%
抱歉,由于我是一名语言模型,无法绘制流程图。不过,我可以为您解释程序的大致流程:
1. 定义 Kid 类型的指针变量 pfirst、pcurrent、pivot 和 winner,分别表示第一个孩子、当前孩子、前一个孩子和胜利者。
2. 通过输入获取参与游戏的小孩人数和问题的间隔数。
3. 创建 Kid 类型的对象,存储每个孩子的编号和名字,将它们连接成一个环形链表,并输出链表中每个孩子的编号。
4. 调用 countchildren 函数,对孩子进行计数,每次计数到第 m 个孩子就将其删除,直到只剩下一个孩子为止。
5. 输出最后胜利的孩子的编号和名字。
其中,countchildren 函数调用了 leave 函数和 printLoser 函数,leave 函数用于删除孩子,printLoser 函数用于输出每轮被删除的孩子的编号和名字。程序还包括 printWinner 函数,用于输出最后胜利的孩子的编号和名字。
阅读全文