uva 127 "Accordian" Patience

模拟,我也不知道开始错在那里,重新敲了一遍就过了

#include<cstdio>
#include<cstring>

struct s
{
    char c[60][5];
    int s;
}card[60];




void deal(int a,int b)
{
    int x=card[a].s;
    int y=card[b].s;
    strcpy(card[a].c[x+1],card[b].c[y]);
    card[a].s++;
    card[b].s--;
    if(card[b].s <= 0)
        for(int i=b;i<=52;i++)
            card[i]=card[i+1];

}


int main()
{
    while(scanf("%s",card[1].c[1])!=EOF)
    {
        if(card[1].c[1][0]=='#')
            break;
        card[1].s=1;
        int i;
        for(i=2;i<=52;i++)
        {
            scanf("%s",card[i].c[1]);
            card[i].s=1;
        }
        int flag=1;
        int nCount=1;
        while (flag) {
            int i;
            for (i=2; card[i].s!=0; i++) {
                    if(i-3>=1){
                int left = card[i-3].s;
                int right = card[i].s;
                if (card[i].c[right][0]==card[i-3].c[left][0]
                    || card[i].c[right][1]==card[i-3].c[left][1]) {

                    deal(i-3, i);
                    flag = 0;
                    break;
                }}
                if(i-1>=1){
                int left = card[i-1].s;
                int right = card[i].s;
                if (card[i].c[right][0]==card[i-1].c[left][0]
                    || card[i].c[right][1]==card[i-1].c[left][1]) {

                    deal(i-1, i);
                    flag = 0;
                    break;
                }}
            }
            nCount = i;
            if (1 == flag)
                flag = 0;
            else
                flag = 1;
        }
        if(nCount==2)
        {
            printf("1 pile remaining: 52\n");
        }
        else
        {
            printf("%d piles remaining:",nCount-1);
            for(i=1;card[i].s!=0;i++)
                printf(" %d",card[i].s);
            putchar('\n');
        }
    }
}


你可能感兴趣的:(uva 127 "Accordian" Patience)