UVa 133:
In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone is arbitrarily chosen as number 1, and the rest are numbered counter-clockwise up to N (who will be standing on 1's left). Starting from 1 and moving counter-clockwise, one labour official counts off k applicants, while another official starts from N and moves clockwise, counting m applicants. The two who are chosen are then sent off for retraining; if both officials pick the same person she (he) is sent off to become a politician. Each official then starts counting again at the next available person and the process continues until no-one is left. Note that the two victims (sorry, trainees) leave the ring simultaneously, so it is possible for one official to count a person already selected by the other official.
Input
Write a program that will successively read in (in that order) the three numbers (N, k and m; k, m > 0, 0 < N < 20) and determine the order in which the applicants are sent off for retraining. Each set of three numbers will be on a separate line and the end of data will be signalled by three zeroes (0 0 0).
Output
For each triplet, output a single line of numbers specifying the order in which people are chosen. Each number should be in a field of 3 characters. For pairs of numbers list the person chosen by the counter-clockwise official first. Separate successive pairs (or singletons) by commas (but there should not be a trailing comma).
Sample input
10 4 3
0 0 0
Sample output
4 8, 9 5, 3 1, 2 6, 10, 7
思路:这道题我采用了双向链表来做,感觉代码写的有点多。主要的难点还是在于这道题是先同时取出链表中的值,再删除。还需要考虑从两边数值一样的时候只需要删除一次就够了。
另外这道题会出现一种情况就是当两个要删除的节点在相邻的时候需要单独考虑
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int value;
struct Node * next;
struct Node * pre;
}Node;
void createLinklist(Node *head,int num)
{
Node *tmp=head;
Node *temp1=head;
int i;
for(i=2;i<=num;i++)
{
Node *q=(Node *)malloc(sizeof(Node));
q->value=i;
q->next=tmp->next;
tmp->next=q;
q->pre=tmp;
tmp=q;
}
tmp->next=temp1;
temp1->pre=tmp;
}
Node *del(Node *head,int num,int position)
{
Node *tmp=head;
int i=0;
if(position==1)
{
if(num==1)
tmp=tmp->pre;
else
{
while(tmp->next)
{
i++;
if(num-1==i)
break;
tmp=tmp->next;
}
}
}
else
{
if(num==1)
tmp=tmp->next;
else
{
while(tmp->pre)
{
i++;
if(num-1==i)
break;
tmp=tmp->pre;
}
}
}
return tmp;
}
int leng(Node* head)
{
Node *q=head;
Node *p=head;
int len=0;
while(q)
{
len++;
q=q->next;
if(q==p)
break;
}
return len;
}
int main()
{
int n,k,m;
while(scanf("%d%d%d",&n,&k,&m)!=EOF)
{
if(n==0)
break;
Node *head=(Node *)malloc(sizeof(Node));
head->value=1;
head->next=NULL;
head->pre=NULL;
createLinklist(head,n);
int len=leng(head);
Node *one=head;
Node *two=head->pre;
Node *a,*b,*free1,*free2;
int flag=1;
while(len>0)
{
a=del(one,k,1);
b=del(two,m,2);
free1=a->next;
free2=b->pre;
int res=0;
if(free1!=free2)
{
if(flag)
{
printf("%3d%3d",free1->value,free2->value);
flag=0;
}
else
printf(",%3d%3d",free1->value,free2->value);
if(a->next!=b)
{
a->next=free1->next;
a->next->pre=a;
b->pre=free2->pre;
b->pre->next=b;
}
else
{
a->pre->next=b->next;
b->next->pre=a->pre;
res=1;
}
len-=2;
}
else
{
if(flag)
{
printf("%3d",free1->value);
flag=0;
}
else
printf(",%3d",free1->value);
a->next=free1->next;
a->next->pre=a;
len--;
}
if(!res)
{
one=a->next;
two=b->pre;
}
else
{
one=a->pre->next;
two=b->next->pre;
}
}
printf("\n");
}
return 0;
}
本文介绍了解决UVa133问题的一种方法,使用双向链表来模拟申请人的排列,并通过两个方向的选择过程来确定被选中进行再培训的申请人顺序。文章提供了完整的C语言实现代码。
1725

被折叠的 条评论
为什么被折叠?



