UVA133 问题程序
来源:互联网 发布:叉叉助手网络连接失败 编辑:程序博客网 时间:2024/04/29 08:08
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define maxn 30
int n,k,m,a[maxn];
int go ( int p,int d,int t)
{
while(t--)
{
do { p=(p+d+n-1)%n +1 ; }
while(a[p]==0);
}
return p;
}
int main()
{
int i,left;
int p1,p2;
while(scanf("%d %d %d",&n,&k,&m)==3 && n)
{
p1 = n,p2 = 1;
for (i =1; i<= n;i++) a[i]=i;
left = n;
while(left)
{
p1 = go(p1,1,k);
p2 = go(p2,-1,m);
printf("%3d",p1);
left--;
if(p2!=p1)
{
printf("%3d",p2);
left--;
}
a[p1]=a[p2]=0;
if(left) printf(",");
}
printf("\n");
}
#include <stdlib.h>
#include <string.h>
#define maxn 30
int n,k,m,a[maxn];
int go ( int p,int d,int t)
{
while(t--)
{
do { p=(p+d+n-1)%n +1 ; }
while(a[p]==0);
}
return p;
}
int main()
{
int i,left;
int p1,p2;
while(scanf("%d %d %d",&n,&k,&m)==3 && n)
{
p1 = n,p2 = 1;
for (i =1; i<= n;i++) a[i]=i;
left = n;
while(left)
{
p1 = go(p1,1,k);
p2 = go(p2,-1,m);
printf("%3d",p1);
left--;
if(p2!=p1)
{
printf("%3d",p2);
left--;
}
a[p1]=a[p2]=0;
if(left) printf(",");
}
printf("\n");
}
}
照着算法经典入门写了一遍但是莫名runtime error。
参考之前猴子选大王可以使用链表来解决。
参考百度链表解决方法:
#include<iostream>#include<cstdio>#include<malloc.h>#include<cstdlib>using namespace std;typedef struct cicle{ int data; struct cicle *next; struct cicle *prior;}list;list *createcyclelist(int n){ list *head=NULL; list *s,*q; s=(list*)malloc(sizeof(list)); head=s; q=s; head->data=0; int i; for(i=1;i<=n;i++) { s=(list*)malloc(sizeof(list)); s->data=i; q->next=s; s->prior=q; q=s; } q->next=head; head->prior=q; return head;}int main(){ int N,k,m; while(cin>>N>>k>>m) { if(N==0&&k==0&&m==0)break; list *h1,*h2; h1=createcyclelist(N); h2=h1; int flag=N; while(flag) { int temp1=0,temp2=0; int i=1; while(true) { if(h1->data==0) h1=h1->next; else { h1=h1->next; i++; } if(i==k&&h1->data!=0) { temp1=h1->data; break; } } int j=1; while(true) { if(h2->data==0) h2=h2->prior; else { h2=h2->prior; j++; } if(j==m&&h2->data!=0) { temp2=h2->data; break; } } if(temp1!=temp2) { flag=flag-2; cout.width(3); cout<<temp1; cout.width(3); cout<<temp2; if(flag)cout<<","; h1->data=0; h2->data=0; } else { flag=flag-1; cout.width(3); cout<<temp1; if(flag)cout<<","; h1->data=0; } } cout<<endl; } return 0;}
0 0
- UVA133 问题程序
- uva133
- uva133
- uva133
- UVA133
- uva133
- UVa133
- UVa133
- UVa133
- uva133
- uva133救济金发放(双向约瑟夫问题)
- uva133 The Dole Queue
- UVA133-救济金发放
- UVA133:The Dole Queue
- UVA133- The Dole Queue
- The Dole Queue UVA133
- UVA133 The Dole Queue
- uva133-The Dole Queue
- 448. Find All Numbers Disappeared in an Array
- 不使用额外变量和数组的插入排序
- 模拟实现strncat
- 【C++】参数的默认值
- html CSS笔记
- UVA133 问题程序
- 张总是不是最帅的人?
- iTerm2 + Oh My Zsh 终端DIY过程
- linux环境下搭建 j2ee环境
- 1060. 爱丁顿数(25)
- Rotate Treap名次树
- UNICODE和UCS
- Python2 新手 编码问题 吐血总结
- linux常用知识点