代码片断(5)(1 位领导批示)
- 2009-04-7
- 分类:C/C++
- 作者:银子
- 1,246 位领导视察
一个函数递归的小例子.
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include "stdio.h" int age(int n) { int c; if(n==1) c=10; else c=age(n-1)+2; return c; } void main() { printf("%d",age(5)); } |
一个函数递归的小例子.
1 2 3 4 5 6 7 8 9 10 11 12 13 | #include "stdio.h" int age(int n) { int c; if(n==1) c=10; else c=age(n-1)+2; return c; } void main() { printf("%d",age(5)); } |
在表单的 onsubmit 事件中绑定 UnloadConfirm.clear 便可清除提示.(表单提交时)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | var msg_unload="您的文章内容还没有进行保存!"; var UnloadConfirm = {}; UnloadConfirm.set = function(confirm_msg) { window.onbeforeunload = function(event) { event = event || window.event; event.returnValue = confirm_msg; } } UnloadConfirm.clear = function() { window.onbeforeunload = function(){}; } UnloadConfirm.set(msg_unload); |
函数使用..求一个数的阶乘
并运算出n!/(m!*(n-m)!)的结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #include "stdio.h" #include "math.h" float fac(int k) { float t=1; int i; for(i=2;i<=k;i++) t*=i; return t; } void main() { float c; int m,n; scanf("%d%d", &m,&n); c=fac(n)/(fac(m)*fac(n-m)); printf("%f",c); } |
起泡排序算法.从小到大排序
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #include "stdio.h"; void main() { int i, j, tmp, arr[6] = {9,5,8,4,0,2}; for(i=1;i<6;i++) { for(j=0;j<6-i;j++) { if(arr[j]>arr[j+1]) { tmp=arr[j]; arr[j]=arr[j+1]; arr[j+1]=tmp; } } } for(i=0;i<6;i++) printf("%d",arr[i]); } |
最近正好用到.就自己写了个.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | function chinese_money($money) { $str = array('零','壹','贰','叁','肆','伍','陆','柒','捌','玖'); $unit = array('零','分','角','元','拾','佰','仟','万','拾','佰','仟','亿','拾','佰','仟'); $money = str_replace(",","",$money); $money = number_format($money,2); $money = str_replace(".","",$money); $money = str_replace(",","",$money); $f = $money; $money = abs($money); if($f != $money) $m = '负'; $len = strlen($money); for ($i=1; $i<=$len; $i++) { $num = substr($money,$i-1,1); $j = strlen($money)+1-$i; $m .= $num . $unit[$j]; } foreach($str as $key=>$val) { $m = str_replace($key,$val,$m); } return $m; } $money = 9123123.12; echo $money . "\n"; echo chinese_money($money); |
求出100以内的奇数之和与偶数之和
1 2 3 4 5 6 7 8 9 10 11 12 | #include "stdio.h" #include "math.h" main() { int i,odd,even; for(i=1,odd=0,even=0;i<=100;i=i+2) { odd += i; even += i+1; } printf("%d %d",odd,even); } |
求两个数的最大公约数与最小公倍数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include "stdio.h" #include "math.h" main() { int m,n,m1,n1,a; scanf("%d,%d",&m,&n); m1=m;n1=n; while(m1%n1) { a=m1%n1; m1=n1; n1=a; } printf("%d, %d",n1, m*n/n1); } |
为了我们的地球,请关灯一小时
时间:2009年3月28日晚8:30-9:30
地点:就在你的家里,就在整个地球!

这个树形类还是相当好用..
不过今天在使用时数据中出现了子节点id(71)小于父节点id(104).导致部分子节点没被存储入数组
修改了一下..应该没有问题了..
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | <?php class Tree { var $data = array(); var $child = array(-1=>array()); var $layer = array(-1=>-1); var $parent = array(); var $num = array(); function setNode($id, $parent, $value,$num=0) { $parent = $parent ? $parent : 0; $this->data[$id] = $value; $this->num[$id] = $num; if (!isset($this->child[$id])) $this->child[$id] = array(); $this->child[$parent][] = $id; $this->parent[$id] = $parent; if (!isset($this->layer[$parent]) && $parent == 0) { $this->layer[$id] = 0; } else { $this->layer[$id] = $this->layer[$parent] + 1; } } function getList(&$tree, $root= 0) { foreach ($this->child[$root] as $key=>$id) { $tree[] = $id; if($this->child[$id]) $this->getList($tree, $id); } } function getValue($id) { if($this->layer[$id]==0) { return $this->data[$id]; } else { return $leftmar.$this->data[$id]; } } function getNum($id) { return $this->num[$id]; } function getbitValue($id) { return $this->data[$id]; } function getLayer($id, $space = false) { return $space ? str_repeat($space, $this->layer[$id]) : $this->layer[$id]; } function getParent($id) { return $this->parent[$id]; } function getParents($id) { while ($this->parent[$id] != -1) { $id = $parent[$this->layer[$id]] = $this->parent[$id]; } ksort($parent); reset($parent); return $parent; } function getChild($id) { return $this->child[$id]; } function getChilds($id = 0) { $child = array($id); $this->getList($child, $id); return $child; } function printData() { return $this->layer; } } ?> |
抽空将原先直接在wp上修改的评论表情做成了插件.
第一次写WordPress插件. WP的hook还是蛮好用的嘛..活活…
下载地址 : www.zdyi.com/wp-content/uploads/2009/03/silver_smilies.rar