Problem Type : Queue
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
int queue[10000],front=0,rear=0,a[1000],top=0,count=0;
void enque(int i)
{
queue[top]=i;
top++;
}
void push(int i)
{
a[count]=i;
count++;
}
int main()
{
int i,j,k,n;
while(scanf("%d",&n)==1)
{
if(n==0)
break;
front=0,rear=0,a[1000],top=0,count=0;
for(i=1; i<=n; i++)
{
enque(i);
}
for(i=0; i<top; i++)
{
if(i%2==1)
{
enque(queue[i]);
}
if(i%2==0)
{
push(queue[i]);
}
}
printf("Discarded cards:");
for(i=0; i<count-1; i++)
{
printf(" %d",a[i]);
if(i!=count-2)
printf(",");
}
printf("\n");
printf("Remaining card:");
printf(" %d",queue[top-1]);
printf("\n");
}
return 0;
}
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
int queue[10000],front=0,rear=0,a[1000],top=0,count=0;
void enque(int i)
{
queue[top]=i;
top++;
}
void push(int i)
{
a[count]=i;
count++;
}
int main()
{
int i,j,k,n;
while(scanf("%d",&n)==1)
{
if(n==0)
break;
front=0,rear=0,a[1000],top=0,count=0;
for(i=1; i<=n; i++)
{
enque(i);
}
for(i=0; i<top; i++)
{
if(i%2==1)
{
enque(queue[i]);
}
if(i%2==0)
{
push(queue[i]);
}
}
printf("Discarded cards:");
for(i=0; i<count-1; i++)
{
printf(" %d",a[i]);
if(i!=count-2)
printf(",");
}
printf("\n");
printf("Remaining card:");
printf(" %d",queue[top-1]);
printf("\n");
}
return 0;
}
No comments:
Post a Comment