数据结构,约瑟夫环
发布网友
发布时间:2022-05-07 01:13
我来回答
共3个回答
热心网友
时间:2023-10-10 11:09
我这个程序可能与你要求不一样,给个参考你吧。
输入总数,开始计算的位置,每次数的数,循环输出被点到的数的顺序,运行没有问题,VC6.0,注释全,User Friendly
#include<stdio.h>
#define MAX 1024
int josephu ( int , int , int ); /* function prototype */
/**
* Function main read three numbers and call function
* josephu by passing them.
*
*/
void main()
{
long num; /* total number of lemps */
long skip; /* skip number */
long begin; /* which lamp to begin with */
printf ( "Please input total number, skip number and begin number:\n"
"(integer numbers required)\n ");
scanf ( "%ld %ld %ld" , &num , &skip , &begin );
if ( num < skip || num <= 0 || skip <= 0 || begin <= 0 || skip >= num || begin >= num ) {
printf ( "Error data input!\n" );
}
else
josephu ( num , skip , begin );
}
/**
* This function output the order of lamps-off.The number of
* total lamps,skip and begin number is input by user.
* Then comment the formal parameters and return value
* as the following:
*
* @param num total number of lamps.
* @param skip skip number.
* @param begin lamp begin with.
* @return 0 indicates program ended successfully.
*/
int josephu ( int num , int skip , int begin )
{
int JOSEPHU[MAX]; /* create array */
int flag = 0; /* If flag is equal to skip, print the current number */
int i; /* loop counter */
begin = begin - 1;
printf ( "\nThe original order of lamps:\n" );
/* Assignment the array and assignment the element not used 0 and finally display it. */
for( i = 0 ; i < num ; i++ ) {
JOSEPHU[i] = i + 1;
printf ( "%d " , JOSEPHU[i] );
}
printf ( "\n\n" );
printf ( "The order of lights off:\n" );
/* JOSEPHU[begin]=JOSEPHU[begin-1] */
for ( i = 2 ; ; i++ ) {
/* If the current element is not equal to the last elemnet of array JOSEPHU,then the next element. */
if ( JOSEPHU[begin] != JOSEPHU[num - 1] )
begin++;
else {
begin = 0;
/* If the current element is equal to the last element,then the first element. */
while ( JOSEPHU[begin] < 0 ) /* When there are negative elements,skip them. */
begin++;
/* If satisfied, print the element and assignment it as negative, and increase the value of flag. */
if ( i % skip == 0 ) {
printf ( "%d " , JOSEPHU[begin] );
JOSEPHU[begin] = -JOSEPHU[begin];
flag++;
}
if ( flag == num ) /* If flag is equal to total number, the loop is broken. */
break;
}/* end else */
/* In or at the end of the array,deal with the negative element. */
while ( JOSEPHU[begin] < 0 && JOSEPHU[begin] != JOSEPHU[num - 1] )
begin++;
while ( JOSEPHU[begin] < 0 && JOSEPHU[begin] == JOSEPHU[num - 1] ) {
begin = 0;
while ( JOSEPHU[begin] < 0 )
begin++;
}/* end while */
if ( i % skip == 0 ) {
printf ( "%d " , JOSEPHU[begin] );
flag++;
JOSEPHU[begin] = -JOSEPHU[begin];
}/* end if */
if ( flag == num )
break;
if ( JOSEPHU[begin] == JOSEPHU[num - 1] ) {
i++;
begin = 0;
while ( JOSEPHU[begin] < 0 ) {
begin++;
}/* end while */
if ( i % skip == 0 ) {
printf ( "%d " , JOSEPHU[begin] );
JOSEPHU[begin] = -JOSEPHU[begin];
flag++;
}
}
}/* end for */
printf ( "\n" );
return 0;
}
热心网友
时间:2023-10-10 11:09
你是我群里的吧。。。。明天上线找我 我给你发个以前我做的约瑟夫环 你看看 明天中午左右 乾坤
热心网友
时间:2023-10-10 11:10
free(p),就是释放了原有的指针,没必要去掉呀!