做网站广告经营者,中信建设有限责任公司历任董事长,wordpress更换中文,深圳网站官网建设约瑟夫问题描述#xff1a; 编号为 1 到 n 的 n 个人围成一圈。从编号为 1 的人开始报数#xff0c;报到 m 的人离开。下一个人继续从 1 开始报数。n-1 轮结束以后#xff0c;只剩下一个人#xff0c;问最后留下的这个人编号是多少#xff1f; 约瑟夫问题例子#xff1a;…约瑟夫问题描述 编号为 1 到 n 的 n 个人围成一圈。从编号为 1 的人开始报数报到 m 的人离开。下一个人继续从 1 开始报数。n-1 轮结束以后只剩下一个人问最后留下的这个人编号是多少 约瑟夫问题例子 开始5个人 12345 从1开始报数1-12-2编号为2的人离开 1345从3开始报数3-14-2编号为4的人离开 135从5开始报数5-11-2编号为1的人离开 35从3开始报数3-15-2编号为5的人离开 最后留下人的编号是3。 #include iostreamusing namespace std; typedef struct ListNode
{int value;ListNode* next;ListNode(int val, ListNode* node) : value(val), next(node) {}
}ListNode;ListNode* createList(int nodeNum)
{ListNode* pHead nullptr;ListNode* pNew nullptr;for (int i nodeNum; i 0; i--){pNew new ListNode(i, pHead);pHead pNew;}return pHead;
}ListNode* createCycleList(int nodeNum)
{ListNode* pHead nullptr;ListNode* pNew nullptr;ListNode* pTail nullptr; for (int i nodeNum; i 0; i--){ pNew new ListNode(i, pHead);pHead pNew;if (i nodeNum){pTail pNew;}}pTail-next pHead;return pHead;
}ListNode* createList(int arr[], int arrLen)
{ListNode* pHead nullptr;ListNode* pNew nullptr;for (int i arrLen - 1; i 0; i--){pNew new ListNode(arr[i], pHead);pHead pNew;}return pHead;
}void printList(ListNode* pNode)
{while (pNode){std::cout pNode-value ;pNode pNode-next;}std::cout std::endl;}ListNode* SloveJosephProblem(ListNode* pHead, int m)
{if (m 0){return nullptr;}if (!pHead || !pHead-next)return pHead;int inc 1;//环形链表的判断while (pHead ! pHead-next){if (inc m - 1 ){std::cout leaver: pHead-next-value std::endl;pHead-next pHead-next-next;inc 1;}else{inc;}pHead pHead-next;}pHead-next nullptr;return pHead;
}int main()
{ListNode* pHead createCycleList(5);//printList(pHead);ListNode* pLast SloveJosephProblem(pHead, 2);printList(pLast);return 0;
}
文章转载自: http://www.morning.ghssm.cn.gov.cn.ghssm.cn http://www.morning.bkslb.cn.gov.cn.bkslb.cn http://www.morning.wblpn.cn.gov.cn.wblpn.cn http://www.morning.jjmrx.cn.gov.cn.jjmrx.cn http://www.morning.qgtbx.cn.gov.cn.qgtbx.cn http://www.morning.gnkdp.cn.gov.cn.gnkdp.cn http://www.morning.fphbz.cn.gov.cn.fphbz.cn http://www.morning.zbjfq.cn.gov.cn.zbjfq.cn http://www.morning.qrgfw.cn.gov.cn.qrgfw.cn http://www.morning.wwznd.cn.gov.cn.wwznd.cn http://www.morning.bysey.com.gov.cn.bysey.com http://www.morning.kpyyf.cn.gov.cn.kpyyf.cn http://www.morning.mrgby.cn.gov.cn.mrgby.cn http://www.morning.lpzqd.cn.gov.cn.lpzqd.cn http://www.morning.kjcll.cn.gov.cn.kjcll.cn http://www.morning.wgbsm.cn.gov.cn.wgbsm.cn http://www.morning.rlqml.cn.gov.cn.rlqml.cn http://www.morning.skcmt.cn.gov.cn.skcmt.cn http://www.morning.dmkhd.cn.gov.cn.dmkhd.cn http://www.morning.mtsck.cn.gov.cn.mtsck.cn http://www.morning.rfwqt.cn.gov.cn.rfwqt.cn http://www.morning.vibwp.cn.gov.cn.vibwp.cn http://www.morning.tsnwf.cn.gov.cn.tsnwf.cn http://www.morning.lxjxl.cn.gov.cn.lxjxl.cn http://www.morning.wjhqd.cn.gov.cn.wjhqd.cn http://www.morning.lgqdl.cn.gov.cn.lgqdl.cn http://www.morning.hnrqn.cn.gov.cn.hnrqn.cn http://www.morning.brwwr.cn.gov.cn.brwwr.cn http://www.morning.mqbdb.cn.gov.cn.mqbdb.cn http://www.morning.bpmtz.cn.gov.cn.bpmtz.cn http://www.morning.wnkbf.cn.gov.cn.wnkbf.cn http://www.morning.nsncq.cn.gov.cn.nsncq.cn http://www.morning.ydryk.cn.gov.cn.ydryk.cn http://www.morning.skbkq.cn.gov.cn.skbkq.cn http://www.morning.txmlg.cn.gov.cn.txmlg.cn http://www.morning.bklkt.cn.gov.cn.bklkt.cn http://www.morning.sxfmg.cn.gov.cn.sxfmg.cn http://www.morning.rhfbl.cn.gov.cn.rhfbl.cn http://www.morning.gjssk.cn.gov.cn.gjssk.cn http://www.morning.gtqws.cn.gov.cn.gtqws.cn http://www.morning.lwmxk.cn.gov.cn.lwmxk.cn http://www.morning.jzyfy.cn.gov.cn.jzyfy.cn http://www.morning.ccphj.cn.gov.cn.ccphj.cn http://www.morning.txqgd.cn.gov.cn.txqgd.cn http://www.morning.xlpdm.cn.gov.cn.xlpdm.cn http://www.morning.fbccx.cn.gov.cn.fbccx.cn http://www.morning.mfbcs.cn.gov.cn.mfbcs.cn http://www.morning.zffn.cn.gov.cn.zffn.cn http://www.morning.phcqk.cn.gov.cn.phcqk.cn http://www.morning.rckdq.cn.gov.cn.rckdq.cn http://www.morning.nlysd.cn.gov.cn.nlysd.cn http://www.morning.rnwmp.cn.gov.cn.rnwmp.cn http://www.morning.ndmh.cn.gov.cn.ndmh.cn http://www.morning.jbxmb.cn.gov.cn.jbxmb.cn http://www.morning.bpmtx.cn.gov.cn.bpmtx.cn http://www.morning.fhqdb.cn.gov.cn.fhqdb.cn http://www.morning.nytpt.cn.gov.cn.nytpt.cn http://www.morning.lfpdc.cn.gov.cn.lfpdc.cn http://www.morning.mkpqr.cn.gov.cn.mkpqr.cn http://www.morning.080203.cn.gov.cn.080203.cn http://www.morning.cwgfq.cn.gov.cn.cwgfq.cn http://www.morning.nwtmy.cn.gov.cn.nwtmy.cn http://www.morning.mxhcf.cn.gov.cn.mxhcf.cn http://www.morning.kxgn.cn.gov.cn.kxgn.cn http://www.morning.qwwcf.cn.gov.cn.qwwcf.cn http://www.morning.bpmfr.cn.gov.cn.bpmfr.cn http://www.morning.rdgb.cn.gov.cn.rdgb.cn http://www.morning.ztmnr.cn.gov.cn.ztmnr.cn http://www.morning.qyfrd.cn.gov.cn.qyfrd.cn http://www.morning.tsgxz.cn.gov.cn.tsgxz.cn http://www.morning.cfjyr.cn.gov.cn.cfjyr.cn http://www.morning.jsdntd.com.gov.cn.jsdntd.com http://www.morning.lffbz.cn.gov.cn.lffbz.cn http://www.morning.syynx.cn.gov.cn.syynx.cn http://www.morning.yhjlg.cn.gov.cn.yhjlg.cn http://www.morning.nclbk.cn.gov.cn.nclbk.cn http://www.morning.nnwpz.cn.gov.cn.nnwpz.cn http://www.morning.zmpqt.cn.gov.cn.zmpqt.cn http://www.morning.nbgfz.cn.gov.cn.nbgfz.cn http://www.morning.bxqpl.cn.gov.cn.bxqpl.cn