急切等待高手援助!1!!!
我在本网站数据结构页面找的约瑟夫环问题在我的环境下显示1>d:\my documents\visual studio 2008\projects\约瑟夫\约瑟夫\约瑟夫.cpp(10) : error C4430: 缺少类型说明符 - 假定为 int。注意: C++ 不支持默认 int
1>生成日志保存在“file://d:\My Documents\Visual Studio 2008\Projects\约瑟夫\约瑟夫\Debug\BuildLog.htm”
1>约瑟夫 - 1 个错误,0 个警告
========== 生成: 成功 0 个,失败 1 个,最新 0 个,跳过 0 个 ==========
我的编程环境是 microsoft visual studio 2008 . 中文版
#include<stdio.h>
#include<malloc.h>
typedef struct lnode
{
int num;
struct lnode *next;
}node,*L;
main()
{
int amount,start,circle,n,c;
L p,l,q;
printf("一共有几个人围成一圈?\n");
scanf("%d",&amount);
getchar();
printf("从第几个开始计数?\n");
scanf("%d",&start);
getchar();
printf("每几人一次循环?\n");
scanf("%d",&circle);
getchar();
l=(L)malloc(sizeof(node)); //头结点
l->next=NULL;
l->num=0;
q=l;
n=0;
while(n++<amount)
{
p=(L)malloc(sizeof(node));
p->next=NULL;
p->num=n;
q->next=p;
q=p;
}
q->next=l->next; //形成循环链表
//以上完成了单向循环链表的建立
p=l->next;
q=l;
n=1;
while(n++<start)
{
p=p->next;
q=q->next;
}
//退出循环时p,q分别位于指定位置
//接下去进行周期性结点删除,直到链表只余一个结点为止
n=1; //n计算被删除的结点的数量,当n=amount-1时删除结束
while(n++<amount)
{
c=1; //c作为循环临时变量
while(c++<circle)
{
p=p->next;
q=q->next;
}
//删除当前p指向的结点
printf("删除结点%d\t",p->num);
q->next=p->next;
p=p->next;
}
printf("\n最后剩下%d\n",p->num);
}
main函数改成这样试一下:
int main()
{
....
...
....
return 0;
} 表达式语法错 l=(L)malloc(sizeof(node)); 改成 #include "stdafx.h"
#include<iostream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int M,N,A;
while(1)
{
cout<<"一共有几个人围成一圈:"<<endl; cin>>M;
cout<<"从第几个开始计数:"<<endl;cin>>A;
cout<<"每几人一次循环:"<<endl;cin>>N;
if(M==0||N==0||A==0) break;
if(M<N||N<0||A<0){cout<<"错误!请重新输入!"<<endl;continue;}
else
{
int r=0;
for (int i = 2; i<=M; i++) r = (r + N) % i;
cout<<"最后剩下:"<<r+A<<endl;
}
}
return 0;
}
约瑟夫环问题不用链表,这样效率会更高
页:
[1]
