2018春招c++游戏岗笔试见了3次以上的一道题

原创、转载请注明出处
编号为1-n的人围成1圈,数到m的人站出来,直到所有人站出来,求站出来的人的序列(key:细节处理)(是的,真正游戏没有可玩性,不用在意)
方法:循环链表
// ChildJumpOrder.cpp: 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include<iostream>
using namespace std;

struct ChainNode
{
    int data;
    ChainNode* next;
};

//编号为1-n的人,数到m的人站出来,求站出来的人的序列(key:细节处理)
int main()
{
    int n, m;
    cin >> n >> m;

//create circularList int len = n; ChainNode* headNode = new ChainNode; ChainNode* pNode = headNode; while (n--) { pNode->data = len - n; if (n) //不需要的不申请 { pNode->next = new ChainNode; pNode = pNode->next; } } pNode->next = headNode; pNode = pNode->next; //print order int rest = len; while (rest) //用rest判断而不用pNode判断,链表是否为空用指针无法判断 { int cot = m - 2; while (cot--) { pNode = pNode->next; } cout << pNode->next->data << endl; ChainNode* tmp = pNode->next; pNode->next = pNode->next->next; delete tmp; rest--; pNode = pNode->next; } return 0; }

 

 

posted @ 2018-04-20 05:32  哲贤  阅读(192)  评论(0编辑  收藏  举报