| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 494 人关注过本帖
标题:C的约瑟夫环问题,有2个问题不懂,请指点.
只看楼主 加入收藏
彼岸箫
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2006-7-19
收藏
 问题点数:0 回复次数:0 
C的约瑟夫环问题,有2个问题不懂,请指点.

以下程序是朋友帮写的,编译测试是正确的,可有2个问题(已加粗)不明白.
#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int *Array;
int t,n,m,i,j,k,x;

struct jose { int data;
int numth;
struct jose *next;
}Node;

struct jose *head,*q,*p,*tempe,*temp,*T;

head = NULL;
printf("Input the number of people:\n");
scanf("%d",&n);
Array = (int*)malloc(sizeof(int) * n );
for( i = 0; i < n; i++)
{ scanf("%d\n",&Array[i]);
if (!head){
head = (struct jose*)malloc( sizeof(*head));
p = head;
p -> data = Array[i];
p -> numth = i + 1;
p -> next = head ;
}
else{
q = (struct jose*)malloc( sizeof(*q));
q -> data = Array[i];
q -> numth = i + 1;
q -> next = p -> next;
p -> next = q;
p = q;
}
};

printf("Input the code-num(o<=m<=20):\n");

do{
scanf("%d",&m);
}while(m<0||m>20);

tempe = p;
for (t = 0 ;t < n;t++)
{ for (j = 1;j < m;j++)
tempe = tempe -> next;


printf("%d",tempe -> next -> numth);

m = tempe -> next-> data;
T = tempe -> next;
tempe -> next = T ->next;
free(T);

}
}

第一个printf应该是在输入M以前出现,为什么编译后是在输入M后出现?

第二个:本身此时循环链表的P是指向最后输入的结点的,而for语句为什么是从后往前数了m-1位?

搜索更多相关主题的帖子: 约瑟夫 
2006-10-23 22:27
快速回复:C的约瑟夫环问题,有2个问题不懂,请指点.
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011325 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved