判斷5張撲克牌的組成

一副牌中發五張撲克牌給你,讓你判斷數字的組成:
有以下幾種情況:
1:四條:即四張一樣數值的牌(牌均不論花色)
2:三條帶一對
3:三條帶兩張不相同數值的牌
4:兩對
5:順子(包括10,J,Q,K,A)
6:什麼都不是

7:只有一對 

分析:

應該不包含大小王,另外J、Q、K、A分別相當於11、12、13、14,雖然從2到A一共是13張牌,但是爲了方便對應,使用了一個包含15個元素的數組來記錄每一個點數出現的次數(捨棄0號元素和1號元素)。

爲了記錄發給你的5張牌,直接用整型肯定不行,不能接受J、Q、K、A的輸入,剛開始的時候準備使用一個字符數組接受輸入,然後再轉換爲整型,但是字符輸入是不能接受10這樣的輸入的,最後只能使用字符串數組接受輸入。

C代碼如下:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define LEN 5
#define TOTAL_LEN 15 

char str_poker[LEN][3];
int int_poker[LEN] = {0};
int total_poker[TOTAL_LEN] = {0};

void check(int len, int poker[]);
void ch_to_int(int len, char str_array[][3], int int_array[]);
void int_sort(int len, int a[]);

int main()
{
	int i;
	printf("please input your cards:");
	for(i=0; i<LEN; i++)
		scanf("%s",str_poker[i]);
	ch_to_int(LEN, str_poker, int_poker);
	printf("the result is:");
	check(LEN, int_poker);
	return 0;
}

void ch_to_int(int len, char str_array[][3], int int_array[])
{
	int i;
	for(i=0; i<LEN; i++)
	{
		if(strcmp(str_array[i],"10") == 0)
		{
			int_array[i] = 10;
			continue;
		}
		if(strcmp(str_array[i],"J") == 0)
		{
			int_array[i] = 11;
			continue;
		}
		if(strcmp(str_array[i],"Q") == 0)
		{
			int_array[i] = 12;
			continue;
		}
		if(strcmp(str_array[i],"K") == 0)
		{
			int_array[i] = 13;
			continue;
		}
		if(strcmp(str_array[i],"A") == 0)
		{
			int_array[i] = 14;
			continue;
		}
		int_array[i] = atoi(str_array[i]);	
	}
}

void int_sort(int len, int a[])
{
	int i,j,temp;
	for(i=0; i<len-1; i++)
		for(j=0; j<len-1; j++)
			if(a[j] > a[j+1])
			{
				temp = a[j];
				a[j] = a[j+1];
				a[j+1] = temp;
			}
}

void check(int len, int poker[])
{
	int i;
	int res = 1;
	for(i=0; i<LEN; i++)
		total_poker[poker[i]]++;
	for(i=1; i<=TOTAL_LEN; i++)
	{
		if(total_poker[i] == 4)
		{
			printf("四條加一張\n");
			return;
		}
		if(total_poker[i] >= 2)
			res *= total_poker[i];
	}
	if(res > 1)
	{
		switch(res)
		{
		case 2:
			printf("一對加三張不同\n");
			break;
		case 3:
			printf("三條加兩張不同\n");
			break;
		case 4:
			printf("兩隊加一張\n");
			break;
		case 6:
			printf("三條加一對\n");
			break;
		}
	}
	else
	{
		int_sort(LEN, poker);
		for(i=0; i<LEN-1; i++)
		{
			if(poker[i+1] != poker[i] + 1)
			{
				printf("什麼都不是\n");
				return;
			}
		}
		printf("五張順子\n");
	}
}


幾組測試用例如下:



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章