zoukankan      html  css  js  c++  java
  • 推断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("四条加一张
    ");
    			return;
    		}
    		if(total_poker[i] >= 2)
    			res *= total_poker[i];
    	}
    	if(res > 1)
    	{
    		switch(res)
    		{
    		case 2:
    			printf("一对加三张不同
    ");
    			break;
    		case 3:
    			printf("三条加两张不同
    ");
    			break;
    		case 4:
    			printf("两队加一张
    ");
    			break;
    		case 6:
    			printf("三条加一对
    ");
    			break;
    		}
    	}
    	else
    	{
    		int_sort(LEN, poker);
    		for(i=0; i<LEN-1; i++)
    		{
    			if(poker[i+1] != poker[i] + 1)
    			{
    				printf("什么都不是
    ");
    				return;
    			}
    		}
    		printf("五张顺子
    ");
    	}
    }


    几组測试用比例如以下:



  • 相关阅读:
    ExtJS小技巧
    Oracle 表的行数、表占用空间大小,列的非空行数、列占用空间大小 查询
    NPM 私服
    IDEA 不编译java以外的文件
    SQL 引号中的问号在PrepareStatement 中不被看作是占位符
    Chrome 浏览器自动填表呈现淡黄色解决
    批量删除Maven 仓库未下载成功.lastupdate 的文件
    Oracle 11g 监听很慢,由于监听日志文件太大引起的问题(Windows 下)
    Hibernate 自动更新表出错 建表或添加列,提示标识符无效
    Hibernate 自动更新表出错 More than one table found in namespace
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6715710.html
Copyright © 2011-2022 走看看