c现代方法8.2节 deal.c程序自己编写

#include 
#include 
#include 
#include 

int read(int count);
//numsuit,代表四种花色,h heart,s spade,c club,s square
const char num_suit[4] = {'h','s','c','s'};
const char num_rank[13] = {'2','3','4','5','6','7','8','9','t','j','q','k','a'};
bool in_hand[4][13] = {false};

int main()
{
int number;
scanf("%d",&number);
read(number);
printf("\n");
        return 0;
}
int read(int count)
{
//种子
srand((unsigned) time(NULL));

int suit;
int rank;
while(count > 0)
  {
     rank = rand() % 13;
     suit = rand() % 4 ;
     if(!in_hand[suit][rank])
        {
           in_hand[suit][rank] = true;
           printf("%c",num_rank[rank]);
           printf("%c  ",num_suit[suit]);
           --count;
        }
  
  }
}
~                                                                               
~     

运行:

5 
7c  qh  js  ac  kc 

c语言现代方法 13章程序设计题3题

#include 
#include 
#include 
#include 

int read(int count);
const char *num_suit[4] = {"heart","spade","club","diamond"};
const char *num_rank[13] = 
{"two","three","four","five","six","seven","eight","nine","ten","eleven","twelve","thirteen","ace"};
bool in_hand[4][13] = {false};

int main()
{
int number;
scanf("%d",&number);
read(number);
printf("\n");
    	return 0;
}
int read(int count)
{
srand((unsigned) time(NULL));
int suit;
int rank;
int cnt = count; 
while(cnt > 0)
  {
     rank = rand() % 13;
     suit = rand() % 4 ;
     if(!in_hand[suit][rank])
        {
	   in_hand[suit][rank] = true;
	   printf("%s",num_rank[rank]);
	   printf(" of ");
	   printf("%s\n",num_suit[suit]);
           --cnt;
	}
/*             根据题意,扑克牌最多52张,大于52张如下处理              */
   if(count > 52 && cnt == (count - 52))
   {printf("number is more than 52,only can print 52 pieces.\n");
	   break;}
  
  }
}

 

你可能感兴趣的:(c语言)