| ||||||||||
| 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 | |||||||||
这道题其实很简单,用栈和队列模拟洗牌相当不错。贴个代码参考:#include"stdio.h"
#define n 150/*如果牌数多于15,这里的n的值还得加。12张牌100就够。*/
int stack[n];
int top=-1;
int queue[n];
int qtop=-1,last=-1;
void push(int data)/*入栈*/
{
if(top!=n-1)
{
top++;
stack[top]=data;
}
else
printf("overflow\n");
}
int pop()/*出栈*/
{
if(top!=-1)
{
return stack[top--];
}
else
printf("overlow\n");
}
void putqueue(int data)/*入队*/
{
if(last!=n-1)
{
last++;
queue[last]=data;
}
else
printf("overlow\n");
}
int getqueue()/*出队*/
{
if(last!=qtop)
{
qtop++;
return queue[qtop];
}
}
void poke(int i)/*洗牌*/
{
int a=0,k=0,j,devalue;
while(k<i)/*有i张牌*/
{
k++;
a=pop();
putqueue(a);
for(j=1;j<=a;j++)/*翻转*/
{
devalue=getqueue();
putqueue(devalue);
}
}
}
int main()
{
int i;
scanf("%d",&i);
while(i--)
{
int j,k,m;
int s;
int a[20];
top=qtop=last=-1;/*这里必须要,改变牌数,要清空栈和队列*/
scanf("%d",&j);
for(k=1;k<=j;k++)
push(k);
poke(j);
for(k=0;k<j;k++)/*获得倒序牌*/
a[k]=getqueue();
for(k=j-1;k>=0;k--)/*顺序输出*/
printf("%d ",a[k]);
printf("\n");
}
return 0;
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator