补了一下模拟题:
1:注意越界的可能,会出现任何错误,所以请确保所有下标不会越界
2:注意每个变量的含义,每一行是否是这个变量!!!!!
3:细心+耐心+好好训练+没时间了
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
typedef long long ll;
char s1[300];
int dp[10][20];
int num[13]={10,2,3,4,5,6,7,8,9,1,11,13,12};
int bian1(char b){
if(b=='A')
return 1;
else if(b=='J')
return 11;
else if(b=='Q')
return 12;
else if(b=='K')
return 13;
}
char bian2(int x){
if(2<=x&&x<=9)
return '0'+x;
else if(x==1)
return 'A';
else if(x==11)
return 'J';
else if(x==12)
return 'Q';
else if(x==13)
return 'K';
}
int next(int a){
if(a==13)return 1;
else return a+1;
}
vector<int>card;
vector<int>new_card;
int minnum(int x){//x有的最小字典序
for(int i=0;i<13;i++){
if(dp[x][num[i]])return num[i];
}
}
int mincount(int x){ //数量最少且字典序最小
int count=15;
int id;
for(int i=0;i<13;i++){
if(dp[x][num[i]]<count&&dp[x][num[i]]){
count=dp[x][num[i]];
id=num[i];
}
}
return id;
}
int has[10];
int challenge(int u,int v,int say){
int mark=0;
for(int i=0;i<new_card.size();i++){
if(new_card[i]!=say)mark=1;
}
if(mark==1){ //挑战成功
for(int i=0;i<card.size();i++)
dp[v][card[i]]++;
has[v]+=card.size();
card.clear();
return 1;
}
for(int i=0;i<card.size();i++) //挑战失败,自己拿完
dp[u][card[i]]++;
has[u]+=card.size();
card.clear();
return 0;
}
void PRINT(){
for(int i=1;i<=4;i++){
if(has[i]==0){
printf("WINNER\n");
}else{
int mark=0;
for(int j=1;j<=13;j++)
for(int k=1;k<=dp[i][j];k++)
if(j==10){
if(mark)
printf(" 10");
else
printf("10");
mark=1;
}
else{
if(mark)
printf(" %c",bian2(j));
else
printf("%c",bian2(j));
mark=1;
}
printf("\n");
}
}
}
int main(){
int i,j,k,f1,f2,f3,f4,t1,t2,t3,t4,m,n;
//freopen("in.txt","r",stdin);
//freopen("out1.txt","w",stdout);
int id,res;
int put;
while(gets(s1)){
int len1=strlen(s1);
// cout << s1<< endl;
memset(has,0,sizeof(has));
memset(dp,0,sizeof(dp));
card.clear();
for(i=0;i<len1;i++){
if(s1[i]=='0'||s1[i]==' ')continue;
has[1]++;
if('1'<=s1[i]&&s1[i]<='9'){
if(s1[i]=='1'){
dp[1][10]++;i++;continue;}
dp[1][s1[i]-'0']++;
continue;
}
dp[1][bian1(s1[i])]++;
}
for(i=2;i<=4;i++){
gets(s1);
//cout << s1<< endl;
len1=strlen(s1);
for(j=0;j<len1;j++){
if(s1[j]=='0'||s1[j]==' ')continue;
//cout<<i <<" " <<j <<" + "<< " "<<has[i]<< endl;
has[i]++;
if('1'<=s1[j]&&s1[j]<='9'){
if(s1[j]=='1'){
dp[i][10]++;j++;continue;}
dp[i][s1[j]-'0']++;
continue;
}
//if((s1[j]=='0'||s1[j]==' ')||(s1[j]>='1'&&s1[j]<='9'))continue;
dp[i][bian1(s1[j])]++;
}
}
int say=1;
//cout <<" before " <<dp[4][12]<< endl;
for(i=1;i<=4;i++){ //4
// cout <<" to "<<i << endl;
if(i==1){
// cout <<" come " <<i << endl;
if(dp[i][say]){ //1放牌
// cout <<" select " <<1 << endl;
dp[i][say]--;has[i]--;
card.push_back(say);new_card.push_back(say);
}else{ //选一个字典序最小的出来
// cout <<" select " <<2 << endl;
id=minnum(i);dp[i][id]--;card.push_back(id);
// cout <<" put" << id << endl;
new_card.push_back(id);has[i]--;
}
if(dp[2][next(say)]==0){//2挑战
// cout <<"2 challenge" <<endl;
res=challenge(2,i,say);
}else if(dp[3][say]==4){ //3有4张就挑战
// cout <<"3 challenge" <<endl;
res=challenge(3,i,say);
}else if(has[1]==0){//4在没有的时候挑战
// cout <<"4 challenge" <<endl;
res=challenge(4,i,say);
}
}else if(i==2){
if(dp[i][say]){ //2放牌,有这张牌
for(j=1;j<=dp[i][say];j++){
card.push_back(say);
new_card.push_back(say);
}has[i]-=dp[i][say];
dp[i][say]=0;
}else{ //没有这张牌,取一个最小的出来就可以了
id=minnum(i);dp[i][id]--;card.push_back(id);
new_card.push_back(id);has[i]--;
}
if(dp[3][say]==4){ //3有三张就挑战
res=challenge(3,2,say);
}else if(has[i]==0){//4在没有的时候挑战
res=challenge(4,i,say);
}else if(new_card.size()+dp[1][say]>4){ //1在2个+大于4的时候挑战
res=challenge(1,2,say);
}
}else if(i==3){
if(dp[i][say]){ //3放牌,有这张牌
for(j=1;j<=dp[i][say];j++){
card.push_back(say);
new_card.push_back(say);
}has[i]-=dp[i][say];dp[i][say]=0;
}else{ //没有这张牌,取一个最小的出来就可以了
id=mincount(i);
for(j=1;j<=dp[i][id];j++){
card.push_back(id);
new_card.push_back(id);
}has[i]-=dp[i][id];
dp[i][id]=0;
}
if(has[i]==0){//4在没有的时候挑战
res=challenge(4,i,say);
}else if(new_card.size()+dp[1][say]>4){ //1在2个+大于4的时候挑战
res=challenge(1,i,say);
}
}else if(i==4){
if(dp[i][say]==3||dp[i][say]==4){ //4放牌,有这张牌
for(j=1;j<=dp[i][say];j++){
card.push_back(say);
new_card.push_back(say);
}has[i]-=dp[i][say];dp[i][say]=0;
}else{ //不够这张牌,取all要求+一个字典序最小的牌
for(j=1;j<=dp[i][say];j++){
card.push_back(say);
new_card.push_back(say);
}has[i]-=dp[i][say];dp[i][say]=0;
if(has[4]){
id=minnum(i);dp[i][id]--;card.push_back(id);
new_card.push_back(id);has[4]--;
}
}
if(new_card.size()+dp[1][say]>4){ //1在2个+大于4的时候挑战
res=challenge(1,i,say);
}else if(dp[1][next(say)]==0){//1必须说谎so挑战
res=challenge(1,i,say);
}else if(dp[3][say]==4){ //3有三张就挑战
res=challenge(3,i,say);
}
}
// cout <<"------------ "<<i<<" -----------" << endl;
// PRINT();
new_card.clear();
say=next(say);
if((has[1]==0||has[2]==0)||(has[3]==0||has[4]==0))break;
if(i==4)
//return 0;
i=0;
}
PRINT();
}
return 0;
}