crazy frog
hy2012_campus · 共 37 篇文章
java实现n位布尔变量的所有组合
递归方式实现: public void coding(int[] b,int n){ if(n==0){ b[n]=0; outBn(b); b[n]=1; outBn(b); }else{ b[n]=0; outBn(b); b[n]=1; outBn(b); } } public void outBn(int[] b){ for(int i=0; i<b.length; i++){ System.out.println(b[i]); } System.out.println(); } 另外一种方式: int[] result = new int[5]; for(int i = 0; i …
2014-12-11 09:27 · 数据结构与算法 · 评论(0)
java实现括号匹配
public boolean bracketMatch(String str){ Stack<Integer> s = new Stack<Integer>(); for(int i = 0; i < str.length(); i++){ char c = str.charAt(i); switch(c){ case '{': case '[': case '(': s.push(Integer.valueOf(c));break; case '}': if(!s.isEmpty()&&((Integer)s.pop()).intValue()=='{'){ break; }else{ r…
2014-12-10 13:45 · 数据结构与算法 · 评论(0)
java实现数制转换
public void baseConversion(int i){ Stack<String> s = new Stack<String>(); while(i > 0){ s.push(i%8+""); i=i/8; } while(!s.isEmpty()){ System.out.print(s.pop()); } } 16进制转换为10进制: int HexToDec(char *s) { char *p = s; //空串返回0。 if(*p == '\0') return 0; //忽略开头的'0'字符 while(*p == '0') p++; int dec = 0; ch…
2014-12-10 12:46 · 数据结构与算法 · 评论(0)
合并两个数组为非递减数组
public int[] merge(int[] a,int[] b){ int pa = pb = pc = 0; int m = a.length; int n = b.length; int[] c = new int[m+n]; while(pa < m && pb < n){ if(a[pa] < b[pb]){ c[pc++] = a[pa++]; }else{ c[pc++] = b[pb++]; } } if(pa < m){ while(pa < m){c[pc++] = a[pa++];} }else{ while(pb<n){c[pc++] = b[pb++];} } …
2014-12-10 12:07 · 数据结构与算法 · 评论(0)
求回文数涉及到的lcs算法实现
来源于:http://www.java3z.com/cwbwebhome/article/article18/report92.html?id=4867 回文词是一种对称的字符串。任意给定一个字符串,通过插入若干字符,都可以变成回文词。现在的任务是,求出将给定字符串变成回文词所需要插入的最少字符数 。比如:“Ab3bd”插入2个字符后可以变成回文词“dAb3bAd”或“Adb3bdA”,但是插入少于2个的字符无法变成回文词。 [输入]: 第一行:字符串的长度N(3 <= N <= 5000) 第二行:需变成回文词的字符串 [输出]: 将给定字符串变成回文词所需要插入的最少字符数 [样例]: …
2014-12-10 11:52 · 数据结构与算法 · 评论(0)
hanoi算法递归非递归以及扩展
这是个汉诺塔程序,在调试的时候,输入的数字最好不要大于15,因为每大一个数所得的结果的步骤都会多一倍。如果你有耐心等待结果的话除外。汉诺塔是在欧洲流行的一种游戏,有a,b,c三个竿。a竿上有若干个由大到小的圆盘,大的在下面,小的在上面,b,c都是空杆,请你把a杆上的圆盘都倒到别的杆上,或b或c,在倒盘的过程中不可以大的压小的,实例程序如下: #include <stdio.h> #include <stdlib.h> void move(char x,char y) { printf("%c-->%c\n",x,y); } void hanoi (int n,char one ,char t…
2014-12-10 11:28 · 数据结构与算法 · 评论(0)
nodejs创建监听与获取封装对象模块
整理与node.js开发指南 var http = require('http'); http.createServer(function(req,res){ res.writeHead(200,{'Content-Type':'text/html'}); res.write('<h1>node.js</h1>'); res.end('<p>hello world</p>'); }).listen(3000); console.log("http server is listening at port 3000."); 运行node app.js后进程不会退出事件循环 修改脚本都需要从新运行…
2014-12-09 16:31 · js · 评论(0)