首页 > 代码库 > n张牌判断是否是顺子

n张牌判断是否是顺子

将大小王看成0

排序 然后求出0的个数 如果0的个数大于差值且不为对子就返回true

#include<iostream>  
//n张牌判断是否是顺子
using namespace std;

int partition(int *number,int start,int end){
	int temp = number[start];
	while(start < end){
		while(start < end && number[end] > temp)
			 end --;
		if(start < end )
		 number[start++] = number[end];
		while(start < end && number[start] < temp)
			start ++;
		if(start < end)
			number[end -- ] = number[start];

		number[start] = temp;
	}
	return end ;
}

void qsort(int *number,int start,int end){
	if(start < end ){
		int mid = partition(number,start,end);
		qsort(number,start,mid-1);
		qsort(number,mid+1,end);
	}
}

bool isContinuous(int *number,int length){
	if(NULL == number || length < 1)
		return false ;
	qsort(number,0,length-1);

	int numberOfZero = 0;
	int numberOfGap = 0;
	int i = 0;

	//统计0的个数
	for(i=0;i<length && number[i] == 0;i++){
		numberOfZero ++;
	}

	int small = numberOfZero;
	int big = small + 1;

	while(big < length){
		//出现对子
		if(number[small] == number[big])
			return false;

		numberOfGap += number[big] - number[small] - 1;

		small = big ;
		++big;

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

int main()  
{  
	int data[]={6,1,2,3,4,8,0};
	cout<<"对原数组进行排序:";
	qsort(data,0, sizeof(data) / sizeof(int) -1);
	for(int i =0;i<sizeof(data) / sizeof(int);i++ ){
		cout << data[i]<<" ";
	}
	cout<<endl;
    cout<<"数组是否是顺子  "<<	isContinuous(data,sizeof(data)/sizeof(int))<<endl;
    return 0;  
}