生活随笔
收集整理的這篇文章主要介紹了
产生不重复的随机牌
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
《C語言程序設計:現代方法》p121例
/***************************************************本程序根據用戶的輸入,生成相應數量的撲克牌。** ************************************************/
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <stdbool.h>#define NUM_SUITS 4
#define NUM_RANKS 13int main(void){int num_cards, suit, rank;//用于記錄牌的數字及花色。const char suit_code[4]={'a','b','c','d'};const char rank_code[13]={'1','2','3','4','5','6','7','8','9', 't', 'J','Q','K'};//用于記錄某個牌是否已經發出去。bool isCardExist[NUM_RANKS][NUM_SUITS]={false};printf("How many cards do you want: ");scanf("%d", &num_cards);printf("Your cards are: ");srand((unsigned long)time(NULL));for(int i=0; i<num_cards; i++){suit=rand()%NUM_SUITS;rank=rand()%NUM_RANKS;if(isCardExist[rank][suit]==false){isCardExist[rank][suit]=true;printf("%c%c ", rank_code[rank], suit_code[suit]);}elsei--;}printf("\n");return 0;
}
轉載于:https://www.cnblogs.com/jediael/archive/2013/02/04/4304251.html
總結
以上是生活随笔為你收集整理的产生不重复的随机牌的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。