洗牌程式 C語言實現 (洗牌 箱子排序)

2021-09-20 21:53:15 字數 3157 閱讀 6053

洗牌程式。 現有

n副撲克牌,將它們打亂堆放在一起。每副撲克牌最多有

52張卡片(假設可能存在丟失或者損壞的情況)。每張卡片有三個域:卡片的組號、卡片的樣式以及卡片的面值。請寫乙個方法按照下面的規則對這一堆撲克牌進行排序:

1、排序規則:首先按照組號排序,然後再按照樣式進行排序,最後按照面值進行排序;

注: 程式借助層級結構;**中給出:5副牌、4種花色、13種牌面 的排序;(具體數值可調)

#include #include #include /*

author:yxp

e-mail:[email protected]

如有問題,歡迎和我聯絡~*/

int now_level = 0; //當前層級

int level = 2; //搭建結構的層數;從0開始

int time = 0; //生成程式的迭代次數

int print_time = 0; //列印的呼叫次數

int counter = 0; //層數計數器

int node = 0; //最底層分類數

struct _level ;

struct _node ;

int build_tree(struct _level** top, int *level_th, int level_num); //生成層次結構

void initalize_level(struct _level** head, int *level_th, int now_level_th); //初始化層

int insert(struct _level** top,struct _node* node,int* level_th); //插入

void print_content(file *fp_w,struct _level** top,int *position_arr); //列印指定位置元素

void print_all(file **fp_w,struct _level** top,int* level_th, int now_level_th);//列印出最底層元素的分布情況

int find_ch_place(int ch,int** positin,int *level_th); //找到字元(10x10)的插入位置

int create_struct_node(struct _node** node,int ch,int *level_th);//建立資料節點

int main(int argc, char *ar**) ; /*數值依次表示:5副牌、4種花色、13種牌面*/ //指定每一層的長度

struct _level* top = null;

build_tree(&top, level_th, level);//建立層級結構

printf ("@sorting...\n");

/*位數---意義---數目--取值*/

/*個位-- 副牌 --- 5 -- 0~4*/

/*十位-- 花色 --- 4 -- 0~3*//*黑紅梅方*/

/*百千位-牌面 --- 13-- 0~12*//*(a~k)*/

int card = 3*1+3*10+12*100;/*由上表,表示屬於第3副牌、第三種花色(方塊)、牌面為k(12)*/

struct _node* node;

create_struct_node(&node,card,level_th); //建立資料節點

insert(&top,node,node->traits); //在層級結構中插入資料節點

printf ("@sort end!\n");

char* w_filename = "result.txt"; //牌的排序結果,輸出到檔案「result.txt」

file* fp_w =fopen(w_filename, "w");

print_all(&fp_w,&top,level_th,now_level);

printf ("@result_file has been sent to you folder! pleae check it!\n");

system("pause");

return 0;}

int find_ch_place(int ch,int** positin,int *level_th)//對字元,找到應插入的位置

(*positin)[i] = temp%gap;

temp/=gap;

gap = 10;

} return 0;}

int create_struct_node(struct _node** node,int ch,int *level_th)//建立資料節點

void print_content(file *fp_w,struct _level** top,int *position_arr)//列印層級結構指定位置的元素

else

}elseelse }

} void print_all(file **fp_w,struct _level** top,int* level_th, int now_level_th)//列印出總體的排序情況,並且輸入到檔案裡。

else

} } }

return; }

int insert(struct _level** top,struct _node* node,int* level_th)//在層級結構中的指定位置插入元素

position = node->traits[now_level];;

if ((*temp2)[position].tail == null)else

return 0;}

int build_tree(struct _level** top, int *level_th, int level_num)//搭建層級結構(樹)

void initalize_level(struct _level** head, int *level_th, int now_level_th)//初始化層級結構的每一層

{ time ++;

int i = 0;

(*head) = (struct _level*)malloc(level_th[now_level_th]*sizeof(struct _level));

for (i=0;i

c語言實現磁碟排程演算法 C語言實現洗牌演算法

首先看一道題目 有乙個大小為100的陣列,裡面的元素是從 1 到 100,隨機從陣列中選擇50個不重複數。用math.random 100,就可以拿到乙個 0 到 99 的隨機數,是不是重複50次就可以了?當然不是,假如,第一次隨機到5,第二次如果再一次隨機到5的話,要求是選擇不重複的數,所以要選出...

洗牌,發牌C程式

洗牌需要隨機函式 rand 如要產生 m,n 範圍內的隨機數num,可用 int num rand n m 1 m 在呼叫rand 函式之前,可以使用srand 函式設定隨機數種子,如果沒有設定隨機數種子,rand 函式在呼叫時,自動設計隨機數種子為1。隨機種子相同,每次產生的隨機數也會相同。所以要...

C語言的洗牌發牌小程式

構建一副撲克牌,並實現洗牌發牌。include include include define num 10000 洗牌混亂度 char color 7 7 1 紅桃 2 黑桃 3 草花 4 方片 5 小王 6 大王 char number 0 不存內容 1 a 2 2 3 3 4 4 5 5 6 6...