题目:从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的。2-10为数字本身,A为1,J为11,Q为12,K为13,而大小王可以看成任意数字。 //============================================================================ // Name : 100题之扑克牌的顺子.cpp // Author : // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> using namespace std; int par(int*a,int low,int high) { int partition=a[low]; while(low<high) { while(low<high&&a[high]>=partition) high--; a[low]=a[high]; while(low<high&&a[low]<=partition) low++; a[high]=a[low]; } a[low]=partition; return low; } void quicksort(int*a,int low,int high) { if(low<high) { int partition=par(a,low,high); quicksort(a,low,partition-1); quicksort(a,partition+1,high); } } void sort(int *a,int length) { quicksort(a,0,length-1); } bool IsShunzi(int *a,int length) { int number=0; for(int i=0;i<length;i++) { if(a[i]==0) number++; } sort(a,length); for(int i=0;i<length;i++) cout<<a[i]<<" "; cout<<endl; int number1=0; bool type=false; for(int i=1;i<length;i++) { if(a[i-1]==0&&!type) continue; else if(a[i-1]) { type=true; } if(a[i]-a[i-1]>1) { number1+=a[i]-a[i-1]-1; } } if(number1<=number) return true; else return false; } int main() { int a[10]={1,0,6,4,3}; cout << IsShunzi(a,5)<< endl; return 0; }