• 华为2016研发工程师-删数字


    删数字

    题目解析:类似于约瑟夫环,可以用链表的思路。
    
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    class Node
    {
    public:
        int data;
        Node *pNext;
    };
    class CircularLinkList
    {
    public:
        Node *head;
        CircularLinkList()
        {
             head = new Node;
             head->data = 0;
             head->pNext = head;
        }
        ~CircularLinkList()
        {
            delete head;
        }
        void CreateLinkList(int n);
    };
    void CircularLinkList::CreateLinkList(int n)
    {
        head->data = n-1;
        Node *pnew,*ptail = head;
        for(int i = n-2;i >= 0; i--)
        {
            pnew = new Node;
            pnew->data = i;
            pnew->pNext = head;
            head = pnew;
        }
        ptail->pNext = head;
    }
    
    int main()
    {
        int x;
        int c = 1;
        cin >> x;
        CircularLinkList LinkList;
        LinkList.CreateLinkList(x);
        Node *currNode = LinkList.head;
        //Node *pdelete;
        while(currNode->pNext != currNode)
        {
             if(c == 2)                                    //遍历两个就删一个
             {
                //pdelete = currNode->pNext;
                currNode->pNext = currNode->pNext->pNext;
                //delete pdelete;
                currNode = currNode->pNext;
                c = 1;
             }
             else                                          //不够两个就继续遍历
             {
                 currNode = currNode->pNext;
                 c++;
             }
        }
        cout << currNode->data << endl;
        return 0;
    }
    
    
  • 相关阅读:
    java获取年份的后两位
    jdbcTemplate的Dao层封装
    beta准备
    福大软工 · 第十一次作业
    Alpha 冲刺 (10/10)
    Alpha 冲刺 (9/10)
    Alpha 冲刺 (8/10)
    Alpha 冲刺 (7/10)
    Alpha 冲刺 (6/10)
    Alpha 冲刺 (5/10)
  • 原文地址:https://www.cnblogs.com/Jesse-Cavendish/p/14528292.html
Copyright © 2020-2023  润新知