財神娛樂首存即享優惠回饋唷~詳情請進👉

【數據布局】1-2 約電腦 老虎機瑟夫環成績

這里放出兩種不同的代碼,一個是先生給的(較為龐大),還有一個是本人寫的。

本人寫的:

#include<iostream>
using namespace std;
struct Node {
    int data;            //數據單位
    Node *link;            //指向下一個結點
};
class Josephus
{
private:
    Node *head,*current;        //head是頭結點,current指向當前結點
    int sum;//存儲鏈表中元素的個數
public:
    Josephus();                //無參組織函數,掃數初始化為空
    Josephus(int Snumber,int strnumber);//有參組織函數,Snumber為總數,strnumber為最先序號
    ~Josephus();//析構函數
    void del(Node *&p);
    void select(int num);//num為距離數
    void print();
};
Josephus::Josephus()
{
    current = NULL;
    head = NULL;
    sum = 0;
}
Josephus::Josephus(int Snumber,int strnumber)
{
    sum = Snumber;
    head = new Node;
    head->data = strnumber;
    current = head;
    int dt = strnumber + 1;
    for (int i = 1; i < Snumber; i++)
    {
        Node *p;
        p = new Node;
        p->data = dt;
        current->link = p;
        current = p;
        dt++;
    }
    current->link = head;

}
void Josephus::del(Node *&p)
{
    Node *d = p->link;
    p->link = p->link->link;
    delete d;
}
void Josephus::select(int num)
{
    int sont = 1;
    Node *p = head;
    while (sum!= 1)
    {
        if (sont % (num-1)六合彩即時  == 0)
        {
            del(p);
            sum--;
            head = p;
        }
        sont++;
        p = p->link;
    }
    
    
}
void Josephus::print()
{
    cout << 公眾剩下的序號有:"大眾 << endl;
    Node *p = head;
    for (int i = 0; i < sum; i++)
    {
        cout << p->data << "大眾 "大眾;
        p = p->link;
    }
    cout << endl;
}
Josephus::~Josephus()
{
    /*Node *p = head->link;
    while (p!=head)
    {
        Node *de = p;
        p = p->link;
        delete de;
    }*/
    head = NULL;
    current = NULL;
}

測試代碼:

#include"大眾LinkList.h公眾
int  main()
{
    int Snum,stnum,num;
    cout << "大眾請輸出總數和最先序號:"大539連碰算法眾;
    cin >> Snum >> stnum;
    Josephus A(Snum,stnum);
    A.print();
    cout << 公眾請輸出距離數:"大眾;
    cin >> num;
    A.select(num);
    A.print();
    system("大眾pause"大眾);
    return 0;
}

實在道理很簡略,便是經由過程輪回鏈表賡續輪回然后刪除就OK

規范代碼:

//Circle.h
#include<iostream>
using namespace std;
template<class T>
struct Node   //結點布局
{
    T data;   //結點數據
    Node*link;
    Node() { link = NULL; }
    Node(T e,Node*next = NULL)
    {
        data = e;
        link = next;
    }
};
template<class T>
class CircleLinkList
{     //不帶表頭結點的輪回鏈表類
private:
    Node<T> *current,*front; //current指向某結點,front指向current先驅
public:
    CircleLinkList() :current(NULL),front(NULL) {} //構建空輪回鏈表
    CircleLinkList(T *d,int  mSize); //行使d數組元素構建輪回鏈表
    ~CircleLinkList();            //析構函數
    bool InsertAfter(const T&x);    //在current所指結點以后拔出x
    bool RemoveCurrent(T&x);    //刪除current所指結點
    void movenext(int n = 1);    //current后移n次
    T GetCurrentData() { return current->data; }
    bool toLocatioin(T &t);//讓current指向t結點,若沒有則current不挪移
    void Output() const;    //輸入輪回鏈表
};  
class Joseph //    約瑟夫環類
{
private:
    int numOfBoy;  //圈中人數
    int startPosition;  //報數肇始點
    int interval;     //報數距離
public:
    Joseph(int boys,int start,int m) :
        numOfBoy(boys),startPosition(start),interval(m) {}//組織函數
    void setNumOfBoy(int num) { numOfBoy = num; }//重置圈中人數
    void setStartPosition(int start) { startPosition = start; }//重置肇始點
    void setInterval(int inter) { interval = inter; }//重置報數距離
    int GetWinner();//求得終極優越者編號
    void print();   //輸入約瑟夫環
};
template<class T>
CircleLinkList<T>::CircleLinkList(T *d,int  mSize)
{    //組織函數,將d數組中的mSize個元素創立輪回鏈表
//采取前插法創立。
    int i;
    Node<T> *p;
    current = new Node<T>;
    current->data = d[mSize - 1];
    front = current;
    for (i = mSize - 2; i >= 0; i--)
    {
        p = new Node<T>(d[i],current);
        current = p;
    }
    front->link = current;
}
template<class T>
CircleLinkList<T>::~CircleLinkList()    //析構函數
{
    while (current != front)//燒毀輪回鏈表
    {
        Node<T> *r = current;
        current = current->link;
        delete r;
    }
    delete current;
}
template<class T>
bool CircleLinkList<T>::InsertAfter(const T&x)
//在current所指結點以后拔出x,current指向x結點
{
    Node<T> *s = new Node<T>(x);
    if (!s)return false;
    if (!current)  //原輪回鏈表為空
        current = front = s->link = s;
    else //原輪回鏈表非空
    {
        s->link = current->link;
        cu捕魚達人電腦版rrent->link = s;
        front = current;
        current = s;
    }
    return true;
}
template<class T>
bool CircleLinkList<T>::RemoveCurrent(T&x) //刪除current所指結點
{
    if (!current)//輪回鏈表為空
        return false;
    x = current->data;
    if (current == front)//鏈表中只有一個元素
    {
        delete current;
        current = front = NULL;
    }
    else
    {
        front->link = current->link;//點竄鏈表指針
        delete current;
        current = front->link;
    }
    return true;
}
template<class T>
void CircleLinkList<T>::Output() const     //輸入輪回鏈表
{
    if (!current)//輪回鏈表為空
        return;
    Node<T> *p = current;
    do
    {
        cout << p->data << "大眾  "大眾;
        p = p->link;
    } while (p != current);
    cout << endl;
}
template<class T>
void CircleLinkList<T>::movenext(int k)  539中二合多少錢  //current后移k次
{
    for (int i = 1; i <= k; i++)
    {
        front = current; // front后移
        current = current->link; //current后移
    }
}
template<class T>
bool CircleLinkList<T>::toLocatioin(T &t)
//將current指向元素為t的結點,若沒有則current不挪移
{
    if (!current)//輪回鏈表為空
        return false;
    Node<T> *current1 = current,*front1 = front;
    while (current1->data != t) //探求元素t
    {
        front1 = current1;
        current1 = current1->link;
        if (current1 == current)// 已經探求一圈沒有元素為t的結點
            return false;
    }
    current = current1; //current指向元素為t的結點
    front = front1;
    return true;
}
int Joseph::GetWinner()//取得終極優越者
{
    CircleLinkList<int> boys;
    for (int i = 0; i < numOfBoy; i++)//創立輪回鏈表,編號依次為1~numOfBoy
    {
        int temp = i + 1;
        boys.InsertAfter(temp);
    }
    boys.toLocatioin(startPosition);   //找到報數出發點
    cout << endl << 公眾依次入列的小孩是:"大眾 << endl;
    for (int i = 1; i < nu六合彩全車mOfBoy; i++)   //numOfBoy-1個小孩出圈
    {
        int x;
        boys.movenext(interval - 1);  //報數
        boys.RemoveCurrent(x); //出圈
        cout << x << "大眾  "大眾;  //輸入出圈編號
    }
    return boys.GetCurrentData();  //返歸優越者編號
}
void Joseph::print()   //輸入約瑟夫環
{
    cout << 公眾圈中人數:"大眾 << numOfBoy << endl;
    cout << "大眾報數肇始點:"大眾 << startPosition << endl;
    cout << "大眾報數距離:"大眾 << interval << endl;
}

【免責聲明】本站內容轉載自互聯網,其相關談吐僅代表作者小我私家概念盡非權勢巨子,不代表本站態度。如您發明內容存在版權成績,請提交相關鏈接至郵箱:,咱們將實時予以處置。