|
[讨论]DEV C++中初学者编程 共同学习者顶起来
这里高手多 我这贴放这里还有意义吗? 都没什么人来顶!! 有种孤单的感觉 |
|
|
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
站一圈时谁都是头,设这圈中设一个猴子为1号,我们把它当第一个猴。 第一步:分配m个数组元素分别存{1 2 3 ... m } 这样保存猴王的编号。 第二步:报数,报到第一个n 时就删除 且m=m-1;此时数组脚标为n-1 第三步:从0到n-2脚标的元素移到数组尾部 第四步:删除上一步已去尾部的数组元素将紧连的元素前移重新构造数组a[m]然后 回第二步继续报数直到m=n; 第五步:输出a[0]猴王的编号 这样一圈猴子的问题就等效于a[m]的一排猴子排队报数的问题了 以6个猴子报3,为例: 数组脚标 0 1 2 3 4 5 数组保存的值 1 2 3 4 5 6 报数 1 2 4 5 6 报到3的删了 后面的前移 当前报数位置在 ^ 位置前的1 2 后移 4 5 6 1 2 整个数组前移 4 5 6 1 2 当前报数位置在 ^ 再重新报数循环 直到 m=n 输出 a[0] |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
找到一个较简捷的算法,写起来好简单了 哈!! #include <stdio.h> int a[100]; /* 猴子的取值范围 m<100 */ int main() { int m,n,i,x=0,j; scanf("%d%d",&m,&n); /* 输入猴子数m<100 报数数值n<100 */ for(i=1;i<=m;i++) a[i-1]=i; /* 对数组a[m]赋值即猴子的编号 */ freopen("out.txt","w",stdout); printf("m=%d\nn=%d\n",m,n); dd:; while(m>n) /* 判断是否选出猴王m<=n就知猴王是a[0] */ { for(i=0;i<=m-n;i++) a[i+n-1]=a[i+n]; m=m-1; for(i=0;i<n-1;i++) a[i+m]=a[i]; m=m+n-1; for(i=0;i<m-1;i++) a[i]=a[i+2]; m=m-n+1; } printf("a[0]=%d",a[0]); } 测试通过!!!! 时间复杂度为 (m-n)*(m-n+n-1+m-1)=(m-n)(2m)=2mm-2mn 取满值为20000次左右 |
|
|
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
请高手指正我哪里错了!!! 输出结果不对!!!!! #include <stdio.h> int a[100]; /* 猴子的取值范围 */ int main() { int m,n,i,x=0,j,y; scanf("%d%d",&m,&n); /* 输入猴子数m<=100 报数数值n<=100 */ for(i=1;i<=m;i++) a[i-1]=i; /* 对数组a[m]赋值即猴子的编号 */ printf("m=%d\nn=%d\n",m,n); freopen("out.txt","w",stdout); dd:; if (m>=n) /* 判断是否选出猴王 m比n小就不去删 */ for(i=x;i<m;i=i+n-1) /* x表示环跳回来时从几开始 */ { for(j=i+n-1;j<m-1;j++) /* 用m-1是因最后数组为a[m-1] */ a[i+n-1]=a[i+n]; /* 第一个报数合条件的是a[i+n-1] */ m--; x=n-m+i-1; if(m-i<n) { for(j=x;j<m-1;j++) a[j]=a[j+1]; goto dd; } } else printf("a[x]=%d",a[x]); } |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
选猴王环状一圈的 没人做吗??? 我先写一个看看 选猴王的题目是:M个猴子没了王,站一圈报数N,报到N的不是猴王要离开圈子,一直报下去,到最后M小于N时去掉报数最大的,最后剩下的就是唯一的猴王. 谁也做一做啊 相互借鉴 |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
环形表分析 分析:要解决这问题,首先需要构造一个环表,构造环的方法很简单,只要存储每个人的下一个即可,当然,最后一个人的下一个就是第一个人,这样就构成了一个环,构造环以后,就可进行删除操作,直到环剩下一个人为止。 |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
这是我一同事上课时留在教师机上的 发来给大家借鉴 例题3, 已知一元二次方程组系数,用C写出求解程序. /* 一元二次方程求解 */ #include "stdio.h" #include "math.h" int main() { freopen("matches.out","w",stdout); int a,b,c; float x1,x2,t; /* t 表示的儿塔 x1 x2为方程两个解 */ scanf("%d%d%d",&a,&b,&c); t=b*b-4*a*c; printf("t=%f\n",t); if(t>0.0) { x1=(-b+sqrt(t))/(2*a); x2=(-b-sqrt(t))/(2*a); printf("t>0\n"); } else if(t==0.0) /* 学生易把=号只写一个 */ { x1=-b/(2*a); x2=x1; printf("t=0\n"); } else if(t<0.0) printf("t<0 No anwser!"); printf("x1=%f\nx2=%f\n",x1,x2); } 纯为提供初学者参阅 , 高手们飘过了 ! |
|
|
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
有想法啊, 不过我觉得都得学一下才行, 毕竟汇编不能很容易的制作东东!! |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
谢谢楼上的支持 |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
若题目改一下:改成站一个圈,有哪位来改改这个源程序啊!!?? 一个动归! |
|
[求助]关于100Kx转正式会员的问题
我还要很久啊 我还要很久啊 我还要很久啊 |
|
[原创]各位煎熬中的临时用户有褔了!!!
支持15楼 多泡泡坛子的好 努力恶补中 哈哈 |
|
[原创]汇编学习手册(申请邀请码)
正学习中 谢过 成正式会员了再下了 |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
线性表练习题2 N个猴子站成一排报数,报数为1的出列;继续报数至最后剩下的为猴王。 /* M个猴子站一排报数N选猴王问题,去掉报数时报1的猴子 */ #include <stdio.h> int a[100]; /* 设定数据输入范围 100 */ int ddel(int m1,int n1) { int x,i,j; d1:; a[0]=0; for(i=1;i<=m1;i++) { x=i%n1; if(x==0) a[i]=0; /* 对数组中报数合条件者打标记0 */ printf("a[i-1]=%d i=%d\n",a[i-1],i); } for(i=0;i<m1;i++) { if(a[i]==0) {for(j=i;j<m1;j++) /* 删标0的数组元素 后面值放前一位 */ {a[j]=a[j+1];printf("a[j]=%d j=%d\n",a[j],j); } m1--; } /* 删一个元素 总长减一 */ else printf("m1=%d a[4]=%d\n",m1,a[4]); /* 检查测试m1是否合要求 */ } if(m1>=n1) goto d1; /* 一队完成,回头重新报数 */ return m1; } int main() { int m,n,i,x; scanf("%d%d",&m,&n); /* 输入猴子数m<=100 报数数值n<=100 */ for(i=1;i<=m;i++) a[i-1]=i; /* 对数组a[m]赋值即猴子的编号 */ printf("%d\n%d\n",m,n); x=m/n; printf("%d\n",x); if (x!=0) /* 判断是否选出猴王 */ getchar(); m=ddel(m,n); /* 删除不是猴王的数组元素 */ printf("%d\n",m); getchar(); for(i=0;i<=m-1;i++) /* 对选出的猴王编号进行输出 */ printf("%d",a[i]); getchar(); } 若题目改一下:改成站一个圈,有哪位来改改这个源程序啊!!?? |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
我改了改就成线性表插入操作了 for(i=n;i>t;i--) a[i]=a[i-1]; a[t]=m; n=n+1; 这是原程序中的删除部分那位改成上面的就成插入操作了,不过插入的数值m要你另外输入 |
|
[讨论]DEV C++中初学者编程 共同学习者顶起来
有小题目源程序的也发过来啊 让我们有更多的读程序学习的机会 |
操作理由
RANk
{{ user_info.golds == '' ? 0 : user_info.golds }}
雪币
{{ experience }}
课程经验
{{ score }}
学习收益
{{study_duration_fmt}}
学习时长
基本信息
荣誉称号:
{{ honorary_title }}
能力排名:
No.{{ rank_num }}
等 级:
LV{{ rank_lv-100 }}
活跃值:
在线值:
浏览人数:{{ visits }}
最近活跃:{{ last_active_time }}
注册时间:{{ user_info.create_date_jsonfmt }}
勋章
兑换勋章
证书
证书查询 >
能力值