C语言编程解决约瑟夫问题
#include"stdio.h"#include<graphics.h>#include<ctype.h>#include<math.h>#defineR150#def...
#include "stdio.h"
#include <graphics.h>
#include <ctype.h>
#include <math.h>
#define R 150
#define X 329
#define Y 219
#define PI 3.1415926
void left(int *p,int n);
void disp(int x,int y,int color,int i);
int W;
int S;
int dx[100],dy[100];
main()
{
int num[100],i,N;
int gd=DETECT,gm;
char *p=NULL;
int x,y;
printf("How many child:\n");
scanf("%d",&N);
printf("From which child:\n");
scanf("%d",&W);
printf("Which num:\n");
scanf("%d",&S);
initgraph(&gd,&gm,"");
for(i=W;i<N;i++)num[i]=i+1;
for(i=0;i<N;i++){
dx[i]=X+R*cos(i*2*PI/N);
dy[i]=Y-R*sin(i*2*PI/N);
disp(dx[i],dy[i],3,i);
}
left(num,N);
getch();
closegraph();
}
void left(int *p,int n)
{
int i,out,count=0;
char *q=NULL;
int j=0;
i=0;
out=0;
while(out<n-1) {
if(*(p+i)!=0)count++;
if(count==S) {
disp(dx[i],dy[i],8,i);
itoa(*(p+i),q,10);
setcolor(2);
circle(15+j*30,460,15);
setcolor(14);
outtextxy(10+j*30,460,q);
j++;
*(p+i)=0;
count=0;
out++;
}
i++;
if(i==n)i=0;
delay(30000);
}
}
void disp(int x,int y,int color,int i)
{
int *p;
setcolor(color);
circle(x,y,15);
itoa(i+1,p,10);
outtextxy(x-2,y-5,p);
}
不能循环 改怎么改 求高手帮忙 展开
#include <graphics.h>
#include <ctype.h>
#include <math.h>
#define R 150
#define X 329
#define Y 219
#define PI 3.1415926
void left(int *p,int n);
void disp(int x,int y,int color,int i);
int W;
int S;
int dx[100],dy[100];
main()
{
int num[100],i,N;
int gd=DETECT,gm;
char *p=NULL;
int x,y;
printf("How many child:\n");
scanf("%d",&N);
printf("From which child:\n");
scanf("%d",&W);
printf("Which num:\n");
scanf("%d",&S);
initgraph(&gd,&gm,"");
for(i=W;i<N;i++)num[i]=i+1;
for(i=0;i<N;i++){
dx[i]=X+R*cos(i*2*PI/N);
dy[i]=Y-R*sin(i*2*PI/N);
disp(dx[i],dy[i],3,i);
}
left(num,N);
getch();
closegraph();
}
void left(int *p,int n)
{
int i,out,count=0;
char *q=NULL;
int j=0;
i=0;
out=0;
while(out<n-1) {
if(*(p+i)!=0)count++;
if(count==S) {
disp(dx[i],dy[i],8,i);
itoa(*(p+i),q,10);
setcolor(2);
circle(15+j*30,460,15);
setcolor(14);
outtextxy(10+j*30,460,q);
j++;
*(p+i)=0;
count=0;
out++;
}
i++;
if(i==n)i=0;
delay(30000);
}
}
void disp(int x,int y,int color,int i)
{
int *p;
setcolor(color);
circle(x,y,15);
itoa(i+1,p,10);
outtextxy(x-2,y-5,p);
}
不能循环 改怎么改 求高手帮忙 展开
1个回答
展开全部
//约瑟夫环出列顺序
#include<stdio.h>
int a[] = {0, 2, 3, 4, 5, 6, 7, 8, 1};
int king(int monkey[], int n, int key) {
int q, p, t;
q = n;
t = 0;
do{
p = a[q];
t = t+1;
if ((t % key) != 0 )
q = p;
else {
a[q] = a[p];
printf("%d ",p);
}
} while(p != a[q]);
return p;
}
int main( )
{
int m,n;
printf("几个猴子谁出列\n");
scanf("%d%d",&m,&n);
king(a,m,n);
printf("<--出列\nThe king is %dth\n", king(a,m,n));
return 0;
}
这是一个解决出列顺序和大王是谁的 不知对你有没有帮助
#include<stdio.h>
int a[] = {0, 2, 3, 4, 5, 6, 7, 8, 1};
int king(int monkey[], int n, int key) {
int q, p, t;
q = n;
t = 0;
do{
p = a[q];
t = t+1;
if ((t % key) != 0 )
q = p;
else {
a[q] = a[p];
printf("%d ",p);
}
} while(p != a[q]);
return p;
}
int main( )
{
int m,n;
printf("几个猴子谁出列\n");
scanf("%d%d",&m,&n);
king(a,m,n);
printf("<--出列\nThe king is %dth\n", king(a,m,n));
return 0;
}
这是一个解决出列顺序和大王是谁的 不知对你有没有帮助
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询