hello C ……

canshui 2010-05-14 08:28:21
在下有天晚上在床上突然想,是不是有大虾们,可以把自己知道的比较经典的算法,说下呢……


such as 搬盘子 (汉诺塔),纠结的环 (约瑟夫环),兔爸妈生兔娃(Fibonacci),wait wait ;some else 螺旋矩阵,选

择排序,恶魔站队(魔方阵),wait wait ……


有实现代码最好喽,懒得敲代码说下名字也八错,嘟 嘟 嘟嘟……

help help 小白喽……嘟 嘟 嘟嘟……

大侠(虾,蛤,牛,神,and 啥……)们,动动 连心的 东西吧,发扬一下咱小白与高手心连心的高尚情操……嘟 嘟 嘟嘟……

我(e),就等着啦……


ps 1 :在下,知道算法狂多,不过还是请 强强的 大家 弱弱的 说出一俩(多多益善) ,让 小小的 在下知道天是广滴,
海是不小滴 啦 啦 啦啦……

ps 2 :不积硅步,无以至千里。不积小流,无以成江海 。

ps 3 铛铛铛铛,韩信点兵喽……

ps 4:感激涕零……
...全文
260 42 打赏 收藏 转发到动态 举报
写回复
用AI写文章
42 条回复
切换为时间正序
请发表友善的回复…
发表回复
weilong8998 2010-05-15
  • 打赏
  • 举报
回复
我手上有好几本算法的电子书,呵呵,里面的算法多了去了,呵呵,可惜我没学
weilong8998 2010-05-15
  • 打赏
  • 举报
回复

/*
about Hanoi the game
*/
#include <graphics.h>
#include <dos.h>
#define MAX_LEVEL STACK_SIZE
int position[MAX_LEVEL+1];
stack *theStack[3];
int depth;
int mode;
int print;
int initgame(int d){
int i;
int x,y;
int h=5;
int w;
initerror();
if(mode){
int gdriver = DETECT, gmode, errorcode;
/* initialize graphics mode */
initgraph(&gdriver, &gmode, "");
setfillstyle(1,7);
}
for(i=0;i<3;i++)
if(!create(&theStack[i]))
break;
if(i!=3){
for(;i>=0;i--)dispose(theStack[i]);
error("initgame(int):can not init stack!\n");
return 0;
}
depth=d;
for(i=d;i;i--){
push(theStack[0],i);
if(mode){
y=200+100-theStack[0]->top*(h+1);
w=i*10;
x=150-w/2;
setcolor(i);
setfillstyle(1,i);
bar(x,y,x+w,y+h);
}
position[i]=0;
}
if(mode){
setcolor(15);
for(i=0;i<3;i++)
rectangle(150+i*150-1,120,150+i*150+1,300);
line(50,300,500,300);
}
return 1;
}

int endgame(){
int i=2;
for(;i>=0;i--)dispose(theStack[i]);
printf("report:");
report();
if(mode)closegraph();
return 1;
}

void show(int p,int from,int to){
int i;
int x,y;
int newx,newy;
int h=5;
int w=p*10;
y=200+100-(theStack[from]->top+1)*(h+1);
x=from*150+150-w/2;
newx=to*150+150-w/2;
newy=200+100-theStack[to]->top*(h+1);
while(y>100){
setcolor(0);
setfillstyle(1,0);
bar(x,y,x+w,y+h);
y-=(h+1);
setcolor(15);
rectangle(150+from*150-1,120,150+from*150+1,300);
setcolor(p);
setfillstyle(1,p);
bar(x,y,x+w,y+h);
delay(10);
}
while(x!=newx){
setcolor(0);
setfillstyle(1,0);
bar(x,y,x+w,y+h);
(x>newx)?x--:x++;
setcolor(p);
setfillstyle(1,p);
bar(x,y,x+w,y+h);
delay(2);
}
while(y<newy){
setcolor(0);
setfillstyle(1,0);
bar(x,y,x+w,y+h);
setcolor(15);
rectangle(150+to*150-1,120,150+to*150+1,300);
y+=(h+1);
setcolor(p);
setfillstyle(1,p);
bar(x,y,x+w,y+h);
delay(10);
}
}

int move(int p){
int t,s;
if(!gettop(theStack[position[p>,&t)){
error("move(int):the stack is empty\n");
return 0;
}
if(t==p){
pop(theStack[position[p>,&t);
if(!mode&&print)printf("%c -> ",'A'+position[p]);
/* another important core line */
s=(position[p]+1+(depth%2?p%2:(p+1)%2) )%3;
if(gettop(theStack[s],&t)&&t<p){
error("move(int):can not move big level above small one\n");
return 0;
}
push(theStack[s],p);
if(mode)show(p,position[p],s);
else if(print)printf("%c\t",'A'+s);
position[p]=s;
}else error("move(int):position error\n");
return 1;
}
int move2(int p){
int t,s;
s=(position[p]+1+(depth%2?p%2:(p+1)%2) )%3;
if(print)printf("%c->%c\t",'A'+position[p],'A'+s);
position[p]=s;
return 1;
}
#include <conio.h>
void main(){
unsigned long i;
unsigned long N=10;
unsigned long p,q;
printf("Welcome to Hanoi\n");
printf("Note that this Hanoi is not write by recurrence!\n");
printf("And not calculate with any stack.\n");
printf("but i want to check if the a is right.\n");
printf("i use 3 stack to show if there is any violent move happens.:)\n");
printf("\nEnter a number as level(1 to 30):");
scanf("%d",&N);
if(N<1||N>30){
printf("error: not 1 to 30\n");
return;
}
printf("\n Select show mode('c' in TEXT 'g' in GRAPHICS)\n");
printf("Note that if the level is to big you'd better not use 'g' for speed.\n");
printf("19 is about 20 seconds. 20 is about double of that. etc.\n");
printf("I test on a intel 166mmx cpu. 30 may be 40*1024 seconds.\n");
printf("wish you succeed!\n");
switch(getch()){
case 'c':
printf("do you want to show the result?(y/n)\n");
printf("print result will be slow!!!\n");
do{
mode=getch();
if(mode=='y')print=1;
if(mode=='n')print=0;
}while(mode!='y'&&mode!='n');
mode=0;
break;
case 'g':mode=1;break;
default:printf("error: neither 'c' nor 'g'\n");return;
}

printf("processing...\n");
initgame(N);
/*
core here!!!
only 8 lines, ha!
here get the level queue
as 1 2 1 3 1 2 1 4 1 2 1 3 1 2 1
*/
for(i=1;i<(1L<<N);i++){
q=1L<<N;p=N+1;
while(q&&i%q){
q>>=1;
p--;
}
if(mode||print)move(p);
else move2(p);
}
printf("ok\n");
getch();
endgame();
}
canshui 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 19 楼 wade_2003 的回复:]
hello LZ!
[/Quote]

严肃,严肃,严肃对待噻……
ForestDB 2010-05-14
  • 打赏
  • 举报
回复
仁者见仁,智者见智,一千个读者就有一千个哈姆雷特。
算法本身,你通了悟了,也就通了悟了。
canshui 2010-05-14
  • 打赏
  • 举报
回复
ps X: 我的本意,只是想知道,一些,基本的算法,然后自己去实现下试试……

我知道网络,但是网络太正式与书面化……

而由各位说出来的话,可能会说出算法一些应该注意的地方!!!……

我的本意,就这样啊……

难道要悲剧了…………~~~~(>_<)~~~~ ~~~~~~~
barryhappy 2010-05-14
  • 打赏
  • 举报
回复
友情帮顶
亮剑_ 2010-05-14
  • 打赏
  • 举报
回复
找本算法大全研究一下
canshui 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 azure110 的回复:]
可以一个个 去google 搜索下现成已写好的 算法,这样贴在这里只求结果 实属 浪费大部分人的时间,若要真的懂得那些算法最好是 自己尝试着写并不断更改 积累 这样将会收益更多..
[/Quote]

有道理……
canshui 2010-05-14
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 louyong0571 的回复:]
总结帖,楼主将来汇总一下,出个报告吧,嘿嘿
[/Quote]

不敢,从没这么想呦……

完全目的:是拓展下自己……
ForestDB 2010-05-14
  • 打赏
  • 举报
回复
TAOCP自己去看吧。
  • 打赏
  • 举报
回复
编程时思路清晰就行,算法也就这回事...
qq675927952 2010-05-14
  • 打赏
  • 举报
回复
up lz ,jf
wade_2003 2010-05-14
  • 打赏
  • 举报
回复
hello LZ!
zenny_chen 2010-05-14
  • 打赏
  • 举报
回复
呵呵,好的算法太多了。
Chen-FDCT、AAN-FDCT、
DES、RSA、中国剩余定理

……
canshui 2010-05-14
  • 打赏
  • 举报
回复
悲剧的一天……

虽然结贴,但仍是期待的……
kevinyujm 2010-05-14
  • 打赏
  • 举报
回复
这些算法都有现成的、成熟的例子。
AlanBruce 2010-05-14
  • 打赏
  • 举报
回复
。。。。。。。

看看
problc 2010-05-14
  • 打赏
  • 举报
回复
找米小国奇想世界里面的AlgorithmExamples_Simp.CHM
sky198306 2010-05-14
  • 打赏
  • 举报
回复
hello word~
xiaoji0507 2010-05-14
  • 打赏
  • 举报
回复
又是数据结构……
唉……
加载更多回复(22)
笫 "丨 快速入门 ……¨ ……… …… ¨…… ……… ……¨ ¨¨¨ ……… ……¨ ¨……¨ … 】 ,l U△ ity简 介 12 运行 Un" 12】 Uiity的 版本 】 22 安装 Unlty 123 在线激活 U1iity 124 运 行 示例 工程 1.2.5 安装 Ⅴ 叔d stud⒗ 创建一个 “ Hello WoHd” 程序 "l22224669 3 4 调试程序 】 4】 显 示 Log 1θ 【 o ll 1,42 设 五断点 ¨¨¨… 小结 第 2扌 21 太空射击游戏 ¨ … … ¨ ¨ … … … … … … … … … ¨ … … … … … … … … … ¨ … … … … 浅谈游戏开发 2.1.l 开始 一个游戏项 目 ¨¨, 2,】 ,2 阶段性戍 采 ……¨…¨ 2】 3 荥划 22223334 21" 编写脚本 2】 5 美术 2⒈ 6 QA浏 试 217 发布游戏 游戏策划 2.2,1 浒戏介绍 .… 14 】 4 14 14 2.2.2 游戏 UI 2.2.3 主 角 22.4 游戏操作 45 22.5 歃 导入美术资源 5 5 3 4 2 2 创建场景 16 u汀 0,抄 手机茁戏开拔 i 241 创建火星背景 …………………¨… ………… …¨…… 】 6 242 设呈摄像机和灯尢 ………… ¨ ¨ ¨ ¨ 20 2,5 创建主角 …¨……¨…………¨¨…¨………¨…………………¨……………¨ 21 25I 创建脚本 ………………………………………………………………………¨ 21 252 控制飞船移 动 …………………¨……… … … · · … … …23 2,5,3 创走亍弹 ………¨¨………… 25 254 创建子弹 Prcfab… ………………… …· ¨ · · ¨ ¨ ¨ 26 255 发射子弹 ~ ¨ ¨ ¨ 27 26 创建敌人 ……………………………………¨……¨…¨… 27 物理碰撞 ¨……¨……¨………………………… ¨ ¨ ¨ 30 ¨ ¨ 28 271 添加碰拄体 ¨ 30 敌人 生成 器 , ……¨………………………41 游戏管理器 标题 界面 ¨ ¨ 48 发布游戏 ……¨ 49 … … … … … · · ¨ ¨ ¨ ¨ ¨ · · ¨ … · … ¨ · · … … … … ¨ ¨ 52 第-人 称 射 击游戏 ¨ … … … … … … … … … … … ¨ … … … … … … … … … … … … … 53 策划 ,… …¨……………¨………¨¨……………¨……¨……灬 ………………… 努 311 游戏介绍 …………………¨…………………………………………………… ⒔ 272 触发碰拄 ………………………………… ¨ ¨.¨32 28 高级敌人 ,… …¨………………………………¨………… 28,l 创建敌人 ………………………¨…¨¨… ¨ ¨ ¨ 34 2.8,2 发射子弹 …………………………………………¨ ¨ 36 声 音与特效 …¨………………¨ 29 2】 o 21】 2,12 213 小结 第 3章 3】 312 U1界 面 ¨ ¨ …¨¨ ………… ¨ ¨ ¨ ¨ ¨53 31.3 主 角 … ¨…¨ …… …… …… 3⒈ 4 敌 人 ¨ … … … … … … … … … … … … … … … … …· … … ¨ ¨ ¨ ¨ 53 游戏场 景 ……………………………………………¨ ¨ . 53 主角 ……………………………………… ~54 331 角 色控 制器 …………………………………………………¨………………… 笕 3,3,2 玮 受 哨 扛 力 △ ¨ ¨ .¨ ¨ ¨ ¨ 3,3,3 式器 ………………¨……………… ¨ ¨ ¨ ¨ 58 ⒊ 4 敌人 …………………………………………¨¨ 341 寻路 ………………………………………¨……………………………………” 342 设 置功 Ln。 … ¨… ……… …… …… … … … … … … … … 63 343 行 为 …… …… …… ……… …… …… ……… ……… …… ¨…… u界 面 ………………¨…¨………………………………………………………… “ 2 3 3 3 5 6 3 3 交互 …………¨………………………………………… ¨ ¨ ¨ ¨ 72 , 0 361 =角 的射 击 3.62
hitCSAPP大作业。 我是Hello,我是每一个程序猿¤的初恋(羞羞……) 却在短短几分钟后惨遭每个菜鸟的无情抛弃(呜呜……),他们很快喜欢上sum、sort、matrix、PR、AI、IOT、BD、MIS……,从不回头。 只有我自己知道,我的出身有多么高贵,我的一生多么坎坷! 多年后,那些真懂我的大佬(也是曾经的菜鸟一枚),才恍然感悟我的伟大! 想当年: 俺才是第一个玩 P2P的: From Program to Process 懵懵懂懂的你笨笨磕磕的将我一字一键敲进电脑存成hello.c(Program),无意识中将我预处理、编译、汇编、链接,历经艰辛,我-Hello一个完美的生命诞生了。 你知道吗?在壳(Bash)里,伟大的OS(进程管理)为我fork(Process),为我execve,为我mmap,分我时间片,让我得以在Hardware(CPU/RAM/IO)上驰骋(取指译码执行/流水线等); 你知道吗?OS(存储管理)与MMU为VA到PA操碎了心;TLB、4级页表、3级Cache,Pagefile等等各显神通为我加速;IO管理与信号处理使尽了浑身解数,软硬结合,才使我能在键盘、主板、显卡、屏幕间游刃有余, 虽然我在台上的表演只是一瞬间、演技看起来还很Low、效果很惨白。 感谢 OS!感谢 Bash!在我完美谢幕后回收了我。 我赤条条来去无牵挂! 我朝 CS(计算机系统-Editor+Cpp+Compiler+AS+LD + OS + CPU/RAM/IO等)挥一挥手,不带走一片云彩! 俺也是 O2O: From Zero-0 to Zero-0。 历史长河中一个个菜鸟与我擦肩而过,只有CS知道我的生、我的死,我的坎坷,“只有 CS 知道……我曾经……来…………过……”————未来一首关于Hello的歌曲绕梁千日不绝 !!

69,380

社区成员

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

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