**题目:从扑克牌中随机抽五张牌,判断是不是一个顺子,即这五张牌是不是连续的。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;
}
}