?? joseph_linklist.cpp
字號:
/*
一、需求分析
1、 本程序利用單向循環鏈表模擬了約瑟夫(Joseph)問題;
2、 本程序采用人機對話方式完成整個問題,按照提示輸入參加的人數(n),每個人的密碼(steps),
以及第一次的報數次數(m);然后對該人按照輸入的順序進行標號,當程序執行完輸出出列人的
編號。
二、調試過程
剛開始使用了模板來寫,但是后來一想沒有這個必要就省去了,在用malloc()函數進行動態分配
時,由于初次使用出現了一些寫法上的問題,但是不夠成什么問題,在調試過程中沒有出現什么錯
誤;
三、測試結果
屏幕輸出如下:
The program will be quit if you the number of player if zero!!!!!!!!!!
Input the number of player (n):7
Input the code of every one:3 1 7 2 4 8 4
Input the initial steps(m):6
The order of people out is:6 1 4 7 2 3 5
Input the number of player (n):7
Input the code of every one:3 1 7 2 4 8 4
Input the initial steps(m):6
The order of people out is:6 1 4 7 2 3 5
Input the number of player (n):7
Input the code of every one:3 2 6 5 3 2 1
Input the initial steps(m):12
The order of people out is:5 1 4 6 2 7 3
Input the number of player (n):1
Input the code of every one:1
Input the initial steps(m):1
The order of people out is:1
Input the number of player (n):1
Input the code of every one:1
Input the initial steps(m):123
The order of people out is:1
Input the number of player (n):0
Press any key to continue
*/
/*******************************************************************************/
/*****************************main program**************************************/
/*******************************************************************************/
struct ListNode
{
int data;
int steps;
ListNode *link;
};
#include<iostream>
using namespace std;
int main()
{
int n,m;
cout<<"The program will be quit if you the number of player if zero!!!!!!!!!!\n";
while( 1)
{
ListNode *Joseph = (ListNode *) malloc (sizeof(ListNode));
cout<<"Input the number of player (n):";
cin>>n;
if( !n )
return 1;
cout<<"Input the code of every one:";
cin>>Joseph->steps;
Joseph->data = 1;
Joseph->link = Joseph;
for( int i=1; i<n; i++ )
{
ListNode *temp = (ListNode *) malloc (sizeof(ListNode));
cin>>temp->steps;
temp->data = i+1;
temp->link = Joseph->link;
Joseph->link = temp;
Joseph = Joseph->link;
}
ListNode *temp = Joseph;
Joseph = Joseph->link;//go to back the first of the List;
cout<<"Input the initial steps(m):";
cin>>m;
cout<<"The order of people out is:";
while(Joseph->link != Joseph)
{
int i(1);
while( i<m )
{
temp = Joseph;
Joseph = Joseph->link;
i++;
}
cout<<Joseph->data<<' ';
m = Joseph->steps;
temp->link = Joseph->link;
free(Joseph);
Joseph = temp->link;
}
cout<<Joseph->data<<endl<<endl;
free(Joseph);
}
return 1;
}
//*****************************************************//
/*
一、需求分析
1、 本程序利用單向循環鏈表模擬了約瑟夫(Joseph)問題;
2、 本程序采用人機對話方式完成整個問題,按照提示輸入參加的人數(n),每個人的密碼(steps),
以及第一次的報數次數(m);然后對該人按照輸入的順序進行標號,當程序執行完輸出出列人的
編號。
二、調試過程
剛開始使用了模板來寫,但是后來一想沒有這個必要就省去了,在用malloc()函數進行動態分配
時,由于初次使用出現了一些寫法上的問題,但是不夠成什么問題,在調試過程中沒有出現什么錯
誤;
三、測試結果
屏幕輸出如下:
The program will be quit if you the number of player if zero!!!!!!!!!!
Input the number of player (n):7
Input the code of every one:3 1 7 2 4 8 4
Input the initial steps(m):6
The order of people out is:6 1 4 7 2 3 5
Input the number of player (n):7
Input the code of every one:3 1 7 2 4 8 4
Input the initial steps(m):6
The order of people out is:6 1 4 7 2 3 5
Input the number of player (n):7
Input the code of every one:3 2 6 5 3 2 1
Input the initial steps(m):12
The order of people out is:5 1 4 6 2 7 3
Input the number of player (n):1
Input the code of every one:1
Input the initial steps(m):1
The order of people out is:1
Input the number of player (n):1
Input the code of every one:1
Input the initial steps(m):123
The order of people out is:1
Input the number of player (n):0
Press any key to continue
*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -