| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
有人用环链表来解决1061这题吗,结果没错,就是不知道怎么判断Impossible.我的代码,高手请指点!#include<stdio.h>
#include<malloc.h>
struct Jose
{
int code; //编号
struct Jose *next; //下一指针
};
int main()
{
int i,j,x,y,m,n,L,step=0;
struct Jose *px,*pCur_a,*pivot_a,*pCur_b,*pivot_b; //pCur为当前结点,pivot为前一结点
scanf("%d%d%d%d%d",&x,&y,&m,&n,&L);
px=(struct Jose *)malloc(L*sizeof(struct Jose));
//===========创建环链表=================
for(i=1;i<=L;i++)
{
px[i-1].next=&px[i%L];
px[i-1].code=i-1;
}
pivot_a=&px[L-2];
pCur_a=&px[L-1];
pivot_b=&px[L-2];
pCur_b=&px[L-1];
for(i=0;i<x-1;i++) //转到开始位置青蛙a
{
pivot_a=pCur_a;
pCur_a=pivot_a->next;
}
for(i=0;i<y-1;i++) //转到开始位置青蛙b
{
pivot_b=pCur_b;
pCur_b=pivot_b->next;
}
//======================================
i=m;
j=n;
while(1)
{
pivot_a=&px[(x-1+i)%L];
pCur_a=pivot_a->next;
pivot_b=&px[(y-1+j)%L];
pCur_b=pivot_b->next;
i+=m;
j+=n;
step++;
if(pCur_a->code==pCur_b->code)
{
printf("%d\n",step);
break;
}
else if(step>100000) //不知道怎么判断Impossible,此处判断错误
{
printf("Impossible\n");
break;
}
}
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator