一堆人报数问题退出问题,问题代码求指点迷津

qinyi810 2007-07-07 10:52:09
20. n个人围成一圈,1,2,3循环报数,报到3的人退出。到最后只余1人,输出最后留下的是第几号(最初的序号,以1起始)。若n=6,则输出1;若n=10,则输出4;若n=100,则输出91。函数int fun(int n)实现上述功能,返回n个人中最后余的1人的起始序号。
#include<stdio.h>
#include<stdlib.h>
{
int i,t,b;
int *a=(int *)malloc(sizeof(int)*(n+1));
for(i=1;i<=n;i++)
a[i]=1;
for(i=1,b=0,t=0;;i++)
{
if(a[i]==1)
{
b++;
if(b==3)
{
b=0;
t++;a[i]=0;
}
}
if(t==n-1) break;
if(i==n) i=0;
}
for(i=1;;i++)
if(a[i]==1) break;
free(a);
return(i);
}
void main()
{int m;
scanf("%d",&m);
printf("n=%d\n",fun(m));
}
旧得快烂掉的题,以前用静态数组做可以正常输出结果,但是现在用动态数组运行都不通过,报错Declaration syntax error,并且提示在定义和#include<stdio.h>之间,就是不知道怎么修正错误,希望诸位高手指点下,第一次发帖,谢谢了
...全文
216 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
匠心零度 2011-05-03
  • 打赏
  • 举报
回复
看样子小伙子要加油啊
hubaixing 2008-07-14
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<iostream.h>

// define
#define MAX_BUFFER_SIZE 5
// Global
int len = MAX_BUFFER_SIZE; // used to restord the length of the array
int mValue = 0; // the number want to leave
// you can do everything is this part, ep: some function to caculate and display ...
void cal()
{
// this function caculate the array and outup the last leave number...
int num[MAX_BUFFER_SIZE]; // the data array to caculate
// init the data array
for(int i=0;i<MAX_BUFFER_SIZE;i++)
{
// 元素值就是各自的编号
num[i] = i+1;
}// end for i
// 定义需要的变量:
int nCount = 0; // 已出列的元素个数,用来结束循环
int m = 0; // 计数器,值为mValue时置0
int mValue = 0; // 用来保存用户输入值,数到mValue时,该元素出列
int res = 0; // 出列的元素
// 用户输入的计数值mValue
scanf("%d",&mValue);
// 出列的元素标记为0,如果数组中的某一元素值为0,则直接跳过该元素
for(int index = 0; index<MAX_BUFFER_SIZE;index++)
{
if(num[index]!=0)
{
// 当数组元素不为0时,则计数器加1
m++;
if(m == mValue)
{
//数到mValue时,元素出列,即把元素值置0;
m = 0;
nCount++; // 出列元素的个数加1
res = num[index]; // restore this value
num[index] = 0; // 元素置0,下次不对该元素进行计数
}//end if m
}// end if
if(nCount == MAX_BUFFER_SIZE) // 当所有元素都出列后,退出循环
{
printf("最后出列的人的编号为:%d\n",res);
break;
}
if(index == (MAX_BUFFER_SIZE-1))
// 注意:在下一个循环时,数组下标必须从0开始,所以此处要为-1(下次循环为index++);
index = -1;
}// end for index
}// end of cal
// end of fun section
// main loop
void main()
{
cal();
} // end main
James(Wood) 2007-07-07
  • 打赏
  • 举报
回复
大佬,你第3行明显是函数体,但你前面的函数名称声明呢?
qinyi810 2007-07-07
  • 打赏
  • 举报
回复
汗~检查半天居然没发现这个...老以为是动态数组出问题了,谢谢Kenmark(fenix)了~
我啃 2007-07-07
  • 打赏
  • 举报
回复
#include<stdlib.h>
后面直接跟花括号?????

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧