面试题目44—扑克牌的顺子

**题目:从扑克牌中随机抽五张牌,判断是不是一个顺子,即这五张牌是不是连续的。2—10为数字本身,A为1,J为11,Q为12,K为13,大小王可以看成任意数字。
代码示例:**

#include<iostream>
#include<time.h>
using namespace std;
const int Num = 14;
struct MyStruct
{
    int n;
    char ch[16];
};
MyStruct mystruct[14] = { { 0, "王" }, { 1, "A" }, { 2, "2" }, { 3, "3" }, { 4, "4" }, { 5, "5" }, { 6, "6" },
{ 7, "7" }, { 8, "8" }, { 9, "9" }, { 10, "10" }, { 11, "J" }, { 12, "Q" }, { 13, "K" } };

void ExchangeSort(int a[], int n)
{
    for (int i = 0; i < n-1; i++)
    {
        int index = i;
        for (int j = i+1; j < n; j++)
        {
            if (a[j] < a[index])
                index = j;
        }
        if (index != i)
        {
            int temp = a[i];
            a[i] = a[index];
            a[index] = temp;
        }
    }
}
bool SqNum(int a[], int n)
{
    ExchangeSort(a, n);

    int count_zero = 0;
    int non_zero_index = 0;
    for (int i = 0; i < n; i++)
    {
        if (a[i] == 0)
        {
            count_zero++;
            non_zero_index = i+1;
        }
    }
    //===
    if (count_zero == 0)
    {
        for (int i = 0; i < n - 1; i++)
            if (a[i + 1] - a[i] != 1)
                return false;
        return true;
    }
    else 
    {
        int count_space = 0;
        for (int i = non_zero_index; i < n - 1; i++)
        {
            if (a[i] == a[i + 1])
            {
                return false;
            }
            else
            {
                count_space += a[i + 1] - a[i] - 1;
            }
        }
        if (count_space == 0)
            return true;
        else if (count_space == count_zero)
            return true;
        else
            return false;
    }
}



int main()
{
    const int n = 5;
    int a[n] = {};

    a[0] = 0;
    a[1] = 6;
    a[2] = 2;
    a[3] = 3;
    a[4] = 4;

    bool flag = SqNum(a, n);
    if (flag)
    {
        for (int i = 0; i < n; i++)
            cout << mystruct[a[i]].ch << " ";
        cout << "是连续的" << n << "张牌!" << endl;
    }
    else
    {
        for (int i = 0; i < n; i++)
            cout << mystruct[a[i]].ch << " ";
        cout << "不是连续的" << n << "张牌!" << endl;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值