當前位置:首頁 » 課程大全 » 約瑟夫環課程設計

約瑟夫環課程設計

發布時間: 2021-02-09 10:47:43

㈠ 約瑟夫問題課程設計

核心代碼如下:
Dim a() As Integer, b() As Integer, m As Integer, n As Integer, j As Integer, k As Integer, c As Integer, x As Integer
n = InputBox("請輸入參加隊列人數")
m = InputBox("請輸入起始密碼")
x = InputBox("密碼的上限為:")
ReDim a(1 To n) As Integer, b(1 To n) As Integer
For i = 1 To n
a(i) = 1
b(i) = Int((x) * Rnd) + 1 '產生每內個人的隨機密碼
Next
While c < n
For j = 1 To n
k = k + a(j) '計數容
If k = m And a(j) <> 0 Then
k = 0 '下一個人從1開始報數
a(j) = 0 '出列
Print "出列人的編號為" & j; ",該人的密碼為" & b(j)
m = b(j)
c = c + 1 '累計出列的人數
End If
Next
Wend
該代碼已經經過調試,完全正確

㈡ 數據結構(C語言)約瑟夫環課程設計,主要要有代碼的流程分析(流程圖等等),還有演算法描述。

#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int password;
int num;
struct Node *next;
}Node,*Link;
void InitList(Link &L)
{
L=(Node *)malloc(sizeof(Node));
if(!L) exit(1);
L->password=0;
L->num=0;
L->next=L;
}
void Creater(int n,Link &L)
{
Link p,q;
q=L;
for(int i=1;i<=n;i++)
{
p=(Node *)malloc(sizeof(Node));
if(!p) exit(1);
printf("請輸入第 %d 個人的密碼 : ",i);
scanf("%d",&p->password);
p->num=i;
L->next=p;
L=p;
}
L->next=q->next;
free(q);
}
void main()
{
printf("*********************約瑟夫環********************* \n");
Link L,p,q;
int n,m;
int a=1;
int b=1;
while(b==1)
{
L=NULL;
InitList(L);
printf("請輸入總人數 N: \n");
scanf("%d",&n);
while(n<=1)
{
printf("輸入的總人數有誤,請重新輸入大於1的總人數:\n");
scanf("%d",&n);
}
printf("請輸入初始的上限值 M (正整數):\n ");
scanf("%d",&m);
while(m<0)
{
printf("輸入上限值有誤,請重新輸入:\n");
scanf("%d",&m);
}
Creater(n,L);
printf("最終出列的順序為 : \n");
p=L;
for(int i=1;i<=n;i++)
{
for(int j=1;j<m;j++)
{
p=p->next;
}
q=p->next;
m=q->password;
printf("%d ",q->num);
p->next=q->next;
free(q);
}
printf("\n");
printf("是否繼續重新輸入運算 (1.繼續 0.退出):\n ");
scanf("%d",&b);
printf("\n\n\n");
}
}

㈢ 《數據結構》課程設計,包括約瑟夫環問題求解和八皇後問題求解兩題。明天下午之前給我答案。。我給100分

約瑟夫環的答案是:
typedef struct node
{int num;
struct node *next;
} linklist;

linklist *creat(int n)
{linklist *s,*p,*head;
int i;
s=(linklist*)malloc(sizeof(linklist));
head=s;
s->num=1;
p=s;
for(i=2;i<=n;i++)
{s=(linklist*)malloc(sizeof(linklist));
s->num=i;
p->next=s;
p=s;
}
p->next=head;
return (head);
}
void slect(linklist *head,int m,int n)
{linklist *p,*q;
int i,t,k=1;
p=head;
t=1;
q=p;
do
{p=q->next;
t=t+1;
if(t%m==0)
{printf("%4d",p->num);
q->next=p->next;
k++;
free(p);
}
else q=p;
}while(k
}

void main()
{int n,m;
linklist *a,*head;
printf("Input the total number\n");
scanf("%d",&n);
printf("Input the number to call:\n");
scanf("%d",&m);
head=creat(n);
slect(head,m)
;
}
至於,八皇後的問題.不太清楚!

㈣ joseph環的C語言程序設計

給,已經編譯運行通過:
#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
int key;//每個人持有的密碼
int num;//這個人的編號
struct Node *next;//指向下一個節點
}Node,*Link;
//=================================
void InitList(Link &L) //創建一個空的鏈表
{
L=(Node *)malloc(sizeof(Node));
if(!L) exit(1);
L->key=0;
L->num=0;
L->next=L;
}
void Creater(int n,Link &L) //初始化鏈表
{
Link p,q;
q=L;
for(int i=1;i<=n;i++)
{
p=(Node *)malloc(sizeof(Node));
if(!p) exit(1);
printf("the key_%d is:",i);
scanf("%d",&p->key);
p->num=i;
L->next=p;
L=p;
}
L->next=q->next;
free(q);
}
void main()
{
Link L,p,q;
int n,x;
L=NULL;
InitList(L);//構造出一個只有頭結點的空鏈表
printf("please input the totle number of people:");
scanf("%d",&n);//總共的人數n
printf("the start key is:");
scanf("%d",&x);//初始密碼為x
Creater(n,L);//建立好一個約瑟夫環
p=L;
for(int i=1;i<=n;i++)
{
for(int j=1;j<x;j++)
p=p->next;
q=p->next;
x=q->key;
printf("%d ",q->num);
p->next=q->next;
free(q);
}
}

㈤ 求助高手 數據結構課程設計約瑟夫環問題

#include <stdio.h>
#include <malloc.h>
#define NULL 0

typedef struct Node
{
int m;//密碼
int n;//序號
struct Node *next;
}Node,*Linklist;

Linklist create(int z) //生成循環單鏈表並返回,z為總人數
{
int i,m;
Linklist H,r,s;
H=NULL;
printf("Output the code:");
for(i=1;i<=z;i++)
{
("\nInput m=");
scanf("%d",&m);
s=(Linklist)malloc(sizeof(Node));
s->n=i;
s->m=0;
printf("%d",s->m);
if(H==NULL)//從鏈表的第一個節點插入
{
H=s;
r=H;
}
else//鏈表的其餘節點插入
{
r->next=s;
r=s;//r後移
}
}//for結束
r->next=H;/*生成循環單鏈表*/
return H;
}

void search(Linklist H,int m0,int z)//用循環鏈表實現報數問題
{ int count=1;//count為累計報數人數計數器
int num=0;//num為標記出列人數計數器
Linklist pre,p;
p=H;
printf("Output the order:");
while(num<z)
{
do{
count++;
pre=p;
p=p->next;
}while(count<m0);
pre->next=p->next;
printf("%d ",p->n);
m0=p->m;
free(p);
p=pre->next;

num++;
}//while結束
}

void main()
{
int m0,z;
Linklist H;
printf("Input z:");//z為總人數
scanf("%d",&z);
H=create(z);//函數調用
printf("\nInput the start code m0=");
scanf("%d",&m0);
search(H,m0,z);
}

㈥ 數據結構 C語言版 課程設計 題目:約瑟夫環

O.O!啥叫課程設計格式~~~這個學期我倒是自己寫過一個約瑟夫環的演算法
#include<iostream.h>
typedef struct Node
{
int data;
struct Node *next;
}*Pointer;
Pointer p,q,s;

Pointer Initlink(Pointer &head)
{
head=new Node;
head->next=head;
return(head);
}

void Creatlink(Pointer &head,int n)
{
p=head;
if(n>=2)
{

for(int i=1;i<=n-1;i++)
{
p->data=i;
s=new Node;
p->next=s;
p=s;

}
p->data=n;
p->next=head;
}
else {
p->data=1;
p->next=head;
}

}

int Length(Pointer &head)
{
int j=1;
p=head;
while(p->next!=head)
{
p=p->next;
j++;

}
return(j);

}

Pointer Find(Pointer &head,int i)
{
p=head;
if(i>1)
{
for(int t=1;t<=i-1;t++)
{
p=p->next;
}
}
else
{
p=head;
}
return p;

}

void Delete(Pointer &head,int m)
{

if(m>1)
{
p=Find(head,m-1);
q=p->next;
p->next=p->next->next;
delete(q);
}
else
{
p=Find(head,Length(head));
p->next=p->next->next;
}
}

void main()
{
int n,m;
Pointer head,s;
Initlink(head);
s=head;
cout<<"請輸入數據總量:"<<endl;
cin>>n;
Creatlink(head,n);
cout<<"請輸入循環次數m:"<<endl;
cin>>m;
cout<<"輸出為:";
for(int temp=0;temp<n;temp++)
{
cout<<Find(head,m)->data<<" ";
s=head;
head=Find(head,m+1);
Delete(s,m);
}

}
這個程序的思想很簡單大體內容分兩塊:1.建立一個長度為n的循環鏈表設。n=8,並將鏈表的數據域分別設置為1,2,3,4,5,6,7,8。這就是約瑟夫環的大體框架。2.從1開始每經過m個數據便刪除該數據並將其輸出。設m=4,則第一個輸出的數字是4,第二個數字是8,第三個數字是5(由於鏈表是循環的),然後依次輸出下去直到全部輸出為止。這就是對鏈表所需要進行的操作。也就最終完成了約瑟夫環~

因此為了達到以上目的,你需要一個建立鏈表的函數及源代碼中的 Creatlink(Pointer &head,int n),還需要一個循環刪除的函數及void Delete(Pointer &head,int m),而完成了這樣的操作還是不行,因為有一些細節還需要處理,因為這個刪除操作是循環進行的,所以你需要在刪除一個數據以後從下一個數據從新開始經過m再刪除,而不是重新從頭結點開始刪除,所以你需要一個查找函數Find(Pointer &head,int i),這個函數的作用是查找鏈表中的第i個節點。這樣我們的函數就最終完成了~~~~

以上只不過是我的一點心得,當然算不上什麼課程設計格式,只是希望可以讓樓主學到些東西而已~

㈦ 數據結構JAVA 約瑟夫環問題 課程設計

public class YSF {

/**
* 約瑟夫環
* @param m 數到m退出
* @param n 共有n個人專
* @param k 從第屬k個開始數
* @return
*/
private static int ysf(int m, int n, int k) {
int result = 0;
for (int i = 2; i <= n; i++) {
result = (result + m) % i;
}
return result + k % n;
}

public static void main(String[] args) {
System.out.println(ysf(3, 500, 1));
}
}

㈧ 數據結構課設(joseph環)用C語言求解

1.用循環.
# include "stdio.h"
# define SIZE 100
main()
{
int m,n,i;
int array[SIZE];
printf("約瑟夫環求解,當前設置最大人數為%d.\n",SIZE);
printf("報數上限:\n");
scanf("%d",&m);
printf("總人數為:\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("第%d人的密碼為:",i+1);
scanf("%d",&array[i]);
}
joseph(array,m,n);
}
int joseph(a,m,n)
int a[],m,n;
{
int b[SIZE]; /*計錄編號數組.*/
int i; /*計數器.*/
int flag=0;
int code; /*刪取人的號碼.*/
int sum=n; /*現存人數.*/
int point=0; /*當前報數人的位置.*/
int num=m;
for(i=0;i<n;i++) /*計錄數組.*/
{
b[i]=i+1;
}
while(sum!=0) /*當人數不為零時繼續循環.*/
{
for(i=1;i<=num;i++) /*進行報數.*/
{

if(point>=sum) /*當前報數位置超過最後一人時從第一人報起.*/
{point=1;}
else point++;

}
num=a[point-1]; /*取密碼.*/
code=b[point-1]; /*取號碼.*/
for(i=point;i<=sum;i++) /*刪去退出的人.*/
{
a[i-1]=a[i];
b[i-1]=b[i];

}
sum--; /*現存總人數.*/
flag++; /*退出的人數.*/
point--;
printf("已退出%d人,退出的人的編號為%d.\n",flag,code);
}

}

2.用鏈表.

# include "stdio.h"
# include "alloc.h"
# define LEN sizeof(struct player)
struct player
{
int num; /*編號*/
int secret; /*密碼*/
struct player *next;
};
int n; /*總人數*/
main()
{
int m;
void create();
void delete();
struct player *head;
head=(struct player *)malloc(LEN);
printf("請輸入第一次的密碼:\n");
scanf("%d",&m);
create(head);
delete(head,m);

}
void create(struct player *head)
{
struct player *p1,*p2,*p;
p1=p2=head;
printf("請輸入編號,密碼,當編號為零時輸入結束.\n");
printf("總人數為:\n");
scanf("%d",&n);
do
{
p=p2;
p2=p1;
printf("編號:");
scanf("%d",&p2->num);
printf("密碼:");
scanf("%d",&p2->secret);
p1=(struct player *)malloc(LEN);
p2->next=p1;
}while(p2->num!=0);
p->next=head;
free(p1);
}
void delete(struct player *head,int m)
{
int i;
struct player *p,*q;
int sum;
p=head;
sum=m;
while(n!=0)
{
for(i=1;i<sum;i++)
{ q=p;
p=p->next;
}
printf("%d,",p->num);
sum=p->secret;
q->next=p->next;
p=p->next;
n=n-1;
}
}



㈨ 約瑟夫環問題vc++6.0

沒懂個人密碼是什麼- -。我做的這個是輸入m和n的。用的鏈表。

#include<iostream>

using std::cout;
using std::cin;

struct node
{
int num;
node *next;
};

struct linklist
{
node *rear;
int n;
};

void add(linklist &L,int i)
{
node *p,*q;
q=new node;
q->num=i;
if (i==1)
{
L.rear=q;
q->next=q;
}
else
{
p=L.rear->next;
L.rear->next=q;
L.rear=q;
q->next=p;
}
}

void create(linklist &L,int &n)
{
for (int i=1;i<=n;i++)
add(L,i);
}

void remove(linklist &L,int &n,int t)
{
node *p;
p=L.rear->next;
int times=1;
while (n!=0)
{
if (times+1==t)
{
times=0;
cout<<p->next->num<<" ";
n--;
p->next=p->next->next;

}
p=p->next;
times++;
}

}

int main(){
linklist L;
int t;
cin>>t>>L.n;
create(L,L.n);
remove(L,L.n,t);
return 0;
}

熱點內容
武漢大學學生會輔導員寄語 發布:2021-03-16 21:44:16 瀏覽:612
七年級學生作文輔導學案 發布:2021-03-16 21:42:09 瀏覽:1
不屑弟高考成績 發布:2021-03-16 21:40:59 瀏覽:754
大學畢業證會有成績單 發布:2021-03-16 21:40:07 瀏覽:756
2017信陽學院輔導員招聘名單 發布:2021-03-16 21:40:02 瀏覽:800
查詢重慶2018中考成績查詢 發布:2021-03-16 21:39:58 瀏覽:21
結業考試成績怎麼查詢 發布:2021-03-16 21:28:40 瀏覽:679
14中醫醫師資格筆試考試成績查分 發布:2021-03-16 21:28:39 瀏覽:655
名著賞析課程標准 發布:2021-03-16 21:27:57 瀏覽:881
北京大學商業領袖高端培訓課程 發布:2021-03-16 21:27:41 瀏覽:919