直接用52个栈来模拟52个牌堆,如果空了就直接删除,注意结构体存储更加方便,其他步骤直接模拟完成即可
#include <bits/stdc++.h> using namespace std; const int N = 55; struct Card{ char a, b; }; int judge(Card c1, Card c2) { if (c1.a == c2.a || c1.b == c2.b) return 1; return 0; } int main() { stack<Card> s[N]; Card card; int n = 0, i; while (scanf("%c%c", &card.a, &card.b) && card.a != ‘#‘) { getchar(); s[n++].push(card); if(n==52) { int m=1,flag; while(1) { for(i=m;i<n;i++) { if(i>=3&&judge(s[i].top(),s[i-3].top())) { flag=1; break; } if(i>=1&&judge(s[i].top(),s[i-1].top())) { flag=2; break; } } if(i==n)break; if(flag==1) { s[i-3].push(s[i].top()); m=i-3; } else { s[i-1].push(s[i].top()); m=i-1; } s[i].pop(); if(s[i].empty()) { for(int j=i;j<n-1;j++) { s[j]=s[j+1]; } while(!s[n - 1].empty()) s[n-1].pop(); n--; } } if (n > 1) printf("%d piles remaining:", n);// 注意输出复数形式 else printf("%d pile remaining:", n); for (int i = 0; i < n; i++) { printf(" %d", s[i].size()); while (!s[i].empty()) s[i].pop(); } printf("\n"); n = 0; } } return 0; }
原文:https://www.cnblogs.com/tscjj/p/13769896.html