C语言新手,我做过的练习题——发牌练习题

该题处自C语言程序设计与方法

其中含有bool类型数组的应用

/*程序负责发一副标准纸牌,每张标准纸牌都有一个花色和一个等级。
   程序需要用户指明手里应该握有几张牌*/
   
#include <stdio.h>
#include <stdlib.h>   //srand 和 rand 函数都出于此
#include <stdbool.h> 
#include <time.h>  //time 函数出于此


#define NUM_SUITS 4
#define NUM_RANKS 13 
int main(void)
{
	bool in_hand[NUM_SUITS][NUM_RANKS] = {false}; 
	int num_cards,rank,suit;
	
	const char rank_code[] = {'2','3','4','5','6','7','8','9','t','j','q','k','a'}; 
	const char suit_code[] = {'c','d','h','s'};  //代表四张牌的花色 
	
	srand((unsigned)time(NULL));
	
	printf("Enter number of cards in hand: ");
	scanf("%d",&num_cards);	
	
	printf("Your cards are : ");
	while(num_cards>0){
		 suit = rand() % NUM_SUITS;
		 rank = rand() % NUM_RANKS;
		 if(!in_hand[suit][rank]){
		 	in_hand[suit][rank] = true;
		 	num_cards --;
		 	printf(" %c %c",suit_code[suit],rank_code[rank]);
		 	
		 } 
		 printf("\n");
	}
	//printf("\n");
	return 0; 
 } 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值