#include "stdafx.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
bool ok(char buf[]) { //判断一个串是不是整数串
if ( strlen(buf) != 14 ) {
return false;
}
int i;
for(i=0; buf[i]; i++) {
if(buf[i] < '1'|| buf[i] > '9')
return false;
}
return true;
}
void bubbleSort(char buf[]) {
int length = strlen(buf);
for (int i = 0; i <= length - 1; i++) {
for (int j = length - 1; j > i; j--) {
if (buf[j] < buf[j-1]) {
int temp = buf[j];
buf[j] = buf[j-1];
buf[j-1] = temp;
}
}
}
}
char* subString(char buf[], int pos, int len) {
char* str = (char*)malloc(len*sizeof(char));
int i;
for (i = 0; i < len; i++) {
str[i] = buf[i+pos];
}
return str;
}
bool check(char buf[], int len) {
if (len == 0 ) {
return true;
}
if (buf[0] == buf[1]) {
char* str = subString(buf, 2, len-2);
if ( check(str, len-2) ) { // 检查一对
return true;
}
if (buf[1] == buf[2]) { // 检查“碰”
char* str1 = subString(buf, 3, len-3);
if ( check(str1, len-3) ) {
return true;
}
if (buf[2] == buf[3]) { // 检查“杠”
char* str2 = subString(buf, 4, len-4);
if ( check(str2, len-4) ) {
return true;
}
else
return false;
}
}
return false;
}
else if (buf[1] == buf[0] + 1 && buf[2] == buf[1] + 1) { // 检查“吃”
char* str3 = subString(buf, 3, len-3);
if ( check(str3, len-3) )
return true;
else
return false;
}
return false;
}
void main(int argc, char* argv[]) {
char str[14];
printf("please input a complete Mahjong Hand (0 to end):");
scanf("%s",str);
while ( strlen(str) != 1 || atoi(str) != 0 ) {
if ( !ok(str) ) {
printf("Wrong format! \n");
}
else {
bubbleSort(str);
printf("The sorted sequence: %s\n", str);
if ( check(str, 14) ) {
printf("You Win! \n");
}
else{
printf("Not Win! \n");
}
}
printf("please input a complete Mahjong Hand (0 to end):");
scanf("%s",str);
}
}
Full Flush C语言实现
最新推荐文章于 2023-03-23 18:43:37 发布