67.俩个闲玩娱乐。
1.扑克牌的顺子
从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的。
2-10为数字本身,A为1,J为11,Q为12,K为13,而大小王可以看成任意数字。
//判断扑克牌的顺子 王 用s代替
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
#include<map>
#include<ctype.h>
using namespace std;
int string_to_int(string &str);
int main()
{
map<string,int> replace;//储存要替换的值
replace["a"]=1;
replace["j"]=11;
replace["q"]=12;
replace["k"]=13;
replace["s"]=-1;
while(1)
{
int king_num=0;
bool flags=false;
typedef map<string,int>::iterator iter;
vector<int> store;
string read;
for(int a=0;a!=5;++a)
{
cin>>read;
iter iter1;
if(isalpha(read[0]))
read[0]=tolower(read[0]);
if((iter1=replace.find(read))!=replace.end()) //如果在的话
{
if(iter1->second==-1)//如果是大王
++king_num;
else
store.push_back(iter1->second);
}
else
{
if(find(store.begin(),store.end(),string_to_int(read))==store.end())
store.push_back(string_to_int(read));
else //如果非大王重复则非顺子
flags=true;
}
}
sort(store.begin(),store.end());
if(*(store.end()-1)-*(store.begin())>=5)//如果最大到最小超过5就为非顺子
flags=true;
if(flags)
cout<<"非顺子"<<endl;
else
cout<<"顺子"<<endl;
}
return 0;
}
int string_to_int(string &str)
{
int total=0,base=1;
for(string::size_type t=0;t!=str.size();++t,base*=10)
total+=str[t]*base;
return total;
}