中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

C語言怎么實現發牌洗牌

發布時間:2022-05-05 10:14:12 來源:億速云 閱讀:146 作者:iii 欄目:開發技術

本文小編為大家詳細介紹“C語言怎么實現發牌洗牌”,內容詳細,步驟清晰,細節處理妥當,希望這篇“C語言怎么實現發牌洗牌”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。

1.模擬發牌程序

通過寫兩個函數模塊來編寫洗牌程序和打印的發牌程序

2.編寫洗牌程序

void shuffle(char cards[][10],int n){
    int i=54;
    char ca,cb,ct[10];
    srand((unsigned)time(NULL));
    
    while(i--){
        ca=rand()%54;
        cb=rand()%54;
        
        strcpy(ct,cards[ca]);
        strcpy(cards[ca],cards[cb]);
        strcpy(cards[cb],ct);        
    }
        
}

3.編寫打印的發牌程序

void printCard(char cards[][10],int n){
    int i,rn=0;
    printf("第一個人:\n");
    for(i=0;i<17;i++){
        if(rn%5==0)
            printf("\n");
        rn++;
        printf("%s      ",cards[i]);        
    }
    printf("\n\n第二個人:\n");
    rn=0;
    for(i=17;i<33;i++){
        if(rn%5==0)
            printf("\n");
        rn++;
        printf("%s      ",cards[i]);        
    }
    printf("\n\n第三個人:\n");
    rn=0;
    for(i=33;i<50;i++){
        if(rn%5==0)
            printf("\n");
        rn++;    
        printf("%s      ",cards[i]);    
    }
    printf("\n\n底牌:\n");
    for(i=51;i<54;i++){
        printf("%s      ",cards[i]);        
    }
    printf("\n");
    getchar();
}

4.在主函數預定義和調用

main(){
    system("color 0f");
    char cards[][10]={
    "紅桃 2","紅桃 3","紅桃 4","紅桃 5","紅桃 6","紅桃 7","紅桃 8","紅桃 9","紅桃 10","紅桃 J","紅桃 Q","紅桃 K","紅桃 A",
    "方塊 2","方塊 3","方塊 4","方塊 5","方塊 6","方塊 7","方塊 8","方塊 9","方塊 10","方塊 J","方塊 Q","方塊 K","方塊 A",
    "梅花 2","梅花 3","梅花 4","梅花 5","梅花 6","梅花 7","梅花 8","梅花 9","梅花 10","梅花 J","梅花 Q","梅花 K","梅花 A",        
    "黑桃 2","黑桃 3","黑桃 4","黑桃 5","黑桃 6","黑桃 7","黑桃 8","黑桃 9","黑桃 10","黑桃 J","黑桃 Q","黑桃 K","黑桃 A",
    "大王","小王" 
    };
    int n = 54;
 
    shuffle(cards,n);
    printCard( cards, n);
        
return 0;
}

5.最終程序

#include<stdio.h>
#include<string.h>
#include<stdlib.h> 
#include<time.h>
void shuffle(char cards[][10],int n);
void printCard(char cards[][10],int n);

main(){
    system("color 0f");
    char cards[][10]={
    "紅桃 2","紅桃 3","紅桃 4","紅桃 5","紅桃 6","紅桃 7","紅桃 8","紅桃 9","紅桃 10","紅桃 J","紅桃 Q","紅桃 K","紅桃 A",
    "方塊 2","方塊 3","方塊 4","方塊 5","方塊 6","方塊 7","方塊 8","方塊 9","方塊 10","方塊 J","方塊 Q","方塊 K","方塊 A",
    "梅花 2","梅花 3","梅花 4","梅花 5","梅花 6","梅花 7","梅花 8","梅花 9","梅花 10","梅花 J","梅花 Q","梅花 K","梅花 A",        
    "黑桃 2","黑桃 3","黑桃 4","黑桃 5","黑桃 6","黑桃 7","黑桃 8","黑桃 9","黑桃 10","黑桃 J","黑桃 Q","黑桃 K","黑桃 A",
    "大王","小王" 
    };
    int n = 54;
 
    shuffle(cards,n);
    printCard( cards, n);
        
return 0;
}

void shuffle(char cards[][10],int n){
    int i=54;
    char ca,cb,ct[10];
    srand((unsigned)time(NULL));
    
    while(i--){
        ca=rand()%54;
        cb=rand()%54;
        
        strcpy(ct,cards[ca]);
        strcpy(cards[ca],cards[cb]);
        strcpy(cards[cb],ct);        
    }
        
}

void printCard(char cards[][10],int n){
    int i,rn=0;
    printf("第一個人:\n");
    for(i=0;i<17;i++){
        if(rn%5==0)
            printf("\n");
        rn++;
        printf("%s      ",cards[i]);        
    }
    printf("\n\n第二個人:\n");
    rn=0;
    for(i=17;i<33;i++){
        if(rn%5==0)
            printf("\n");
        rn++;
        printf("%s      ",cards[i]);        
    }
    printf("\n\n第三個人:\n");
    rn=0;
    for(i=33;i<50;i++){
        if(rn%5==0)
            printf("\n");
        rn++;    
        printf("%s      ",cards[i]);    
    }
    printf("\n\n底牌:\n");
    for(i=51;i<54;i++){
        printf("%s      ",cards[i]);        
    }
    printf("\n");
    getchar();
}

6.1程序結果

C語言怎么實現發牌洗牌

6.2

C語言怎么實現發牌洗牌

讀到這里,這篇“C語言怎么實現發牌洗牌”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

海盐县| 揭阳市| 苏尼特右旗| 黑山县| 旌德县| 横山县| 广水市| 潞城市| 万盛区| 青州市| 云林县| 开化县| 新邵县| 伊宁市| 弋阳县| 桂平市| 邯郸市| 仁怀市| 水城县| 彰化市| 肇东市| 瓮安县| 红原县| 金昌市| 怀宁县| 冷水江市| 荔浦县| 祁阳县| 娱乐| 昭通市| 循化| 乌拉特中旗| 正阳县| 阿勒泰市| 香格里拉县| 嘉禾县| 博兴县| 内乡县| 新巴尔虎右旗| 界首市| 托克逊县|