《剑指offer》第六十一题:扑克牌的顺子

// 面试题61:扑克牌的顺子
// 题目:从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的。
// 2~10为数字本身,A为1,J为11,Q为12,K为13,而大、小王可以看成任意数字。

#include <cstdio>
#include <cstdlib>

int Compare(const void* arg1, const void* arg2);

bool IsContinuous(int* numbers, int length)
{
    if (numbers == nullptr || length < 0)
        return false;

    qsort(numbers, length, sizeof(int), Compare);

    int numberOfZero = 0;  //大小王个数
    for (int i = 0; i < length && numbers[i] == 0; i++)
        ++numberOfZero;  //忘了for循环可以多个条件


    int numberOfGap = 0;
    for (int i = numberOfZero; i < length - 1; i++)  //从第一个不是0的数字开始
    {
        if (numbers[i + 1] == numbers[i])  //如果出现对子, 则不可能是顺子
            return false;

        numberOfGap += (numbers[i + 1] - numbers[i] - 1);  //差2 == 间隙1
    }

    return (numberOfZero >= numberOfGap) ? true : false;
}

int Compare(const void* arg1, const void* arg2)
{
    return *(int*) arg1 - *(int*) arg2;
}
// ====================测试代码====================
void Test(const char* testName, int* numbers, int length, bool expected)
{
    if (testName != nullptr)
        printf("%s begins: ", testName);

    if (IsContinuous(numbers, length) == expected)
        printf("Passed.\n");
    else
        printf("Failed.\n");
}

void Test1()
{
    int numbers[] = { 1, 3, 2, 5, 4 };
    Test("Test1", numbers, sizeof(numbers) / sizeof(int), true);
}

void Test2()
{
    int numbers[] = { 1, 3, 2, 6, 4 };
    Test("Test2", numbers, sizeof(numbers) / sizeof(int), false);
}

void Test3()
{
    int numbers[] = { 0, 3, 2, 6, 4 };
    Test("Test3", numbers, sizeof(numbers) / sizeof(int), true);
}

void Test4()
{
    int numbers[] = { 0, 3, 1, 6, 4 };
    Test("Test4", numbers, sizeof(numbers) / sizeof(int), false);
}

void Test5()
{
    int numbers[] = { 1, 3, 0, 5, 0 };
    Test("Test5", numbers, sizeof(numbers) / sizeof(int), true);
}

void Test6()
{
    int numbers[] = { 1, 3, 0, 7, 0 };
    Test("Test6", numbers, sizeof(numbers) / sizeof(int), false);
}

void Test7()
{
    int numbers[] = { 1, 0, 0, 5, 0 };
    Test("Test7", numbers, sizeof(numbers) / sizeof(int), true);
}

void Test8()
{
    int numbers[] = { 1, 0, 0, 7, 0 };
    Test("Test8", numbers, sizeof(numbers) / sizeof(int), false);
}

void Test9()
{
    int numbers[] = { 3, 0, 0, 0, 0 };
    Test("Test9", numbers, sizeof(numbers) / sizeof(int), true);
}

void Test10()
{
    int numbers[] = { 0, 0, 0, 0, 0 };
    Test("Test10", numbers, sizeof(numbers) / sizeof(int), true);
}

// 有对子
void Test11()
{
    int numbers[] = { 1, 0, 0, 1, 0 };
    Test("Test11", numbers, sizeof(numbers) / sizeof(int), false);
}

// 鲁棒性测试
void Test12()
{
    Test("Test12", nullptr, 0, false);
}

int main(int argc, char* argv[])
{
    Test1();
    Test2();
    Test3();
    Test4();
    Test5();
    Test6();
    Test7();
    Test8();
    Test9();
    Test10();
    Test11();
    Test12();

    return 0;
}
测试代码

分析:建模以后真简单。

class Solution {
public:
    bool IsContinuous( vector<int> numbers ) {
        
        int length = numbers.size();
        
        if (length < 1)
            return false;
        
        sort(numbers.begin(), numbers.end());
        
        int numberOfZero = 0;
        for (int i = 0; i < length && numbers[i] == 0; ++i)
            ++numberOfZero;
        
        int numberOfGap = 0;
        for (int i = numberOfZero; i < length - 1; ++i)
        {
            if (numbers[i + 1] == numbers[i])
                return false;
            
            numberOfGap += (numbers[i + 1] - numbers[i] - 1);
        }
        return (numberOfZero >= numberOfGap)? true:false;
    }
};
牛客网提交代码

 

posted @ 2020-04-12 18:07  源周率  阅读(121)  评论(0编辑  收藏  举报