TA的每日心情 | 无聊 2016-2-18 12:58 |
---|
签到天数: 114 天 [LV.6]常住居民II
|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?注册
x
我在学校用VC++调试一段程序,结果和书上的一样
但我用家里安装的VC++调试出来就不一样了
同样是一段程序啊,怎么结果不一样呢??
源程序如下:
#include <iostream.h>
#include <stdlib.h>
typedef int ElemType;
struct LNode
{
ElemType data;
LNode *next;
};
void josephus(int n, int m, int s)
{
LNode *HL=new LNode ;
HL->next=HL;
int i,j;
for(i=n;i>0;i--)
{
LNode *newptr= new LNode;
newptr->data=i;
newptr->next=HL->next;
HL->next=newptr;
}
LNode *ap=HL,*cp=HL->next;
for(i=1;i<s;i++)
{
ap=cp;
cp=cp->next;
if(cp==HL)
{
cp=HL->next;
ap=HL;
}
}
for(i=1;i<n;i++)
{
for(j=1;j<m;j++)
{
ap=cp;
cp=cp->next;
//cout<<cp->data<<endl;
if(cp==HL)
{
cp=HL->next;
ap=HL;
}
}
cout<<cp->data<<\" \";
ap->next=cp->next;
delete cp;
cp=ap->next;
if(cp==HL)
{
cp=HL->next;
ap=HL;
}
}
cout<<HL->next->data<<endl;
delete HL->next;
delete HL;
}
void main()
{int n,m,s;
cout<<\"input three numbers\"<<endl;
cin>>n>>m>>s;
josephus(n,m,s);}
|
|
|
|
|
|
|