uva127 "Accordian" Patience

简介: uva127 "Accordian" Patience
#include <stdio.h>#include <stdlib.h>#define LOCAL#define ERROR 0#define OK 1typedefstructdata{
charrank;
charsuit;
structdata*next;
}Data;
typedefstructlnode{
intcount;
Data*top;
structlnode*next;
}LNode, *LinkList;
LinkListinitList();
intGetElem(LNode*h, inti, LNode**e);
LNode*GetPrecursor(LinkListh, LNode*e);
intmatch(Data*a, Data*b);
voidfreespace(LNode*h);
intmain()
{
intans[52];
inti;
intfirst;
charstr[3];
LinkListh=NULL;
intcount=0;
intstatus;
LNode*node , *e=NULL, *p=NULL, *pre=NULL;
Data*q=NULL;
intflag;
Data*datanode=NULL;
intpiles;
#ifdef LOCALfreopen("c://uva_in.txt", "r", stdin);
//freopen("c://uva_out.txt", "w", stdout);#endifwhile (1)
    {
scanf("%s", str);
if (str[0] =='#')
break;
if (count==0)
h=initList();
datanode= (Data*)malloc(sizeof(Data));
datanode->rank=str[0];
datanode->suit=str[1];
datanode->next=NULL;
node= (LNode*)malloc(sizeof(LNode));
node->top= (Data*)malloc(sizeof(Data));
node->top->next=datanode;
node->count=1;
node->next=h->next;
h->next=node;
p=h->next;
while (1)
        {
flag=1;
while (flag)
            {
flag=0;
status=GetElem(p, 3, &e);
if (status==ERROR|| (status==OK&&!match(p->top->next, e->top->next)))   
status=GetElem(p, 1, &e);
if (status==OK&&match(p->top->next, e->top->next))
                {
flag=1;
q=p->top->next;
p->top->next=q->next;
q->next=e->top->next;
e->top->next=q;
e->count++;
p->count--;
if (p->count==0)
                    {
pre=GetPrecursor(h, p);
pre->next=p->next;
free(p);
                    }
p=e;
                }
            }
pre=GetPrecursor(h, p);
if (pre!=h)
p=pre;
elsebreak;
        }
count++;
if (count==52)
        {
p=h->next;
i=0;
while (p)
            {
ans[i++] =p->count;
p=p->next;
            }
piles=i;
if (piles==1)
printf("%d pile remaining: ", piles);
elseprintf("%d piles remaining: ", piles);
for (i=piles-1, first=1; i>=0; i--)
            {
if (first)
first=0;
elseprintf(" ");
printf("%d", ans[i]);
            }
printf("/n");
freespace(h);
count=0;
        }
    }
return0;
}
LinkListinitList()
{
LNode*p= (LNode*)malloc(sizeof(LNode));
p->top=NULL;
p->next=NULL;
returnp;
}
intGetElem(LNode*h, inti, LNode**e)
{
LNode*p=h;
intj=0;
while (p&&j<i)
    {
p=p->next;
j++;
    }
if (!p)
returnERROR;
*e=p;
returnOK;
}
//获得该结点的前驱LNode*GetPrecursor(LNode*h, LNode*e)
{
LNode*p=h;
while (p->next!=e)
p=p->next;
returnp;
}
intmatch(Data*a, Data*b)
{
if (a->rank==b->rank||a->suit==b->suit)
return1;
elsereturn0;
}
voidfreespace(LNode*h)
{
LNode*p;
Data*q;
if (h->next)
    {
p=h->next;
while (p->top->next)
        {
q=p->top->next;
p->top->next=q->next;
free(q);
        }
free(p->top);
freespace(h->next);
    }
free(h);
}
目录
相关文章
Uva10001 Garden of Eden
Uva10001 Garden of Eden
60 0
UVa10123 No Tipping
UVa10123 No Tipping
68 0
uva10112 Myacm Triangles
uva10112 Myacm Triangles
51 0
|
机器学习/深度学习
uva 10273 Eat or Not to Eat?
点击打开链接uva 10273 思路: 暴力求解 分析: 1 题目要求没有吃掉的奶牛的个数已经最后一次吃掉奶牛的天数 2 没有其它的方法只能暴力,对于n头牛的n个周期求最小公倍数,然后在2个公倍数之内暴力求解 代码: #inclu...
836 0
uva 1394 - And Then There Was One
点击打开链接uva 1394 思路: 数学递推 分析: 1 题目是一道变形的约瑟夫环变形问题 2 网上看到一篇很好的数学递推法 问题描述:n个人(编号0~(n-1)),从0开始报数,报到(m-1)的退出,剩下的人继续从0开始报数。
1006 0
|
人工智能
uva 11300 - Spreading the Wealth
点击打开链接uva 11300 思路:数学分析+贪心 分析: 1 首先最终每个人的金币数量可以计算出来,它等于金币总数除以人数n。接下来我们用m来表示每人的最终的金币数 2 现在假设编号为i的人初始化为Ai枚金币,Xi表示第i个人给第i-1个人Xi枚金币,对于第一个人来说他是给第n个人。
873 0
|
机器学习/深度学习 并行计算 AI芯片
刘汝佳uva 字符串专题
第一题   palindrome 点击打开链接uva 401 题目意思:给定一个字符串判断是什么类型 分析: 1 根据输出我们知道这个字符串总共有4种类型 2 首先应该是否是“palindrome ”,判断的理由很简单直接对这个...
1142 0