递归练习题
来源:互联网 发布:base t以太网端口 编辑:程序博客网 时间:2024/06/05 10:42
Java 语言
1. Write a recursive method that returns the number of 1‟s in the binary representation of N. Use the fact that is equal to the number of 1‟s in the representation of N/2, plus 1, if N is odd.
public static int NumberOfN(int n){ if(n == 1){ return 1; }else if(n % 2 == 0){ return NumberOfN(n / 2); }else{ return NumberOfN((n - 1) / 2) + 1; }}
2. Write the routines wise the following declarations:
public void permute( String str );private void permute( char [ ] str, int low, int high )The first routine is a driver that calls the second and prints all the
permutations of the characters in String str. If str is “abc”, then the strings that are output are abc, acb, bac, bca, cab,and cba. Use recursion for the second routine.
调用方法省略
private static void permute(char[ ] list , int low, int high) { int i; char t; if (low == high) { for (i = 0; i <= high; i++) { System.out.print(list[i]); } }else { for (i = low; i <= high; i++) { t = list[low]; list[low] = list[i]; list[i] = t; perm(list, low + 1, high); t = list[low]; list[low] = list[i]; list[i] = t; } } }
3. 已知a[n]为整型数组,试写出实现下列运算的递归算法。
1)求数组a中的最大整数。
2)求n个整数的平均值。
private int max(int x[], int n) { if(n == 1){ return x[0]; }else{ if(x[n - 1] > max(x, n - 1)){ return x[n - 1]; }else{ return max(x, n - 1); } } }
4. Check recursively if the following objects are palindromes:
a. A word
b. a sentence ( ignoring blanks, lower- and uppercase differences, and punctuation marks so that “Madam, I‟m Adam” is accepted as a palindrome)
private boolean palindrome(String str){ str = str.toLowerCase(); for(int i = 0; i < str.length(); i++){ if(str.charAt(i) < 'a' || str.charAt(i) > 'z'){ str = str.replace(str.charAt(i), ' '); } } str = str.replace(" ", ""); System.out.println(str); if(str.length() != 0 && (str.charAt(0) != str.charAt(str.length() - 1))){ return false; }else{ if(str.length() == 1 || str.length() == 0){ return true; }else { return palindrome(str.substring(1,str.length() - 1)); } } }
5. 找出从自然数 1, 2, ..., n 中任取r个数的所有组合, 编一个递归算法.
例子: n = 5 r =3
1 2 3 4 5 5 4 3
542 541 532 531 521 432 431 421 321
<pre name="code" class="java"> private static int a[]; private void call(){ System.out.println("Enter n"); Scanner input = new Scanner(System.in); int n = input.nextInt(); a = new int[n]; System.out.println("Enter r"); int r = input.nextInt(); a[0] = r; comb(n, r); }
private void comb(int n, int r){ int i,j; if(r < 0 || r > n) return; for(i = n; i >= r; i--) { a[r] = i; if (r > 1) { comb(i - 1, r - 1); }else{ for(j = a[0]; j > 0; j--){ System.out.print(a[j]); } System.out.println(); } } }
6. HANOI TOWER
private static void hanoi(){ System.out.println( "Enter number of disks" ); Scanner input = new Scanner(System.in); int n = input.nextInt(); System.out.println("The move are:"); moveDISKs(n, 'A', 'B', 'C'); } private static void moveDISKs(int n, char fromTower, char toTower, char auxTower) { if (n == 1) { System.out.println("Move disk " + n + " from " + fromTower + " to " + toTower); }else { moveDISKs(n - 1, fromTower, auxTower, toTower); System.out.println("Move disk " + n + " from " + fromTower + " to " + toTower); moveDISKs(n - 1, auxTower, toTower, fromTower); } }
0 0
- 递归练习题
- 1364 递归练习题
- 链表|递归练习题
- 递归、回溯练习题
- 递归、回溯练习题答案
- 递归算法练习题
- 【C语言】递归练习题小结
- 递归递推练习题–L12
- 递归递推练习题–M13
- 递归递推练习题–N14
- 多重循环 方法和递归练习题+参考答案
- 几个递归练习题问题(迷宫,算24,小游戏,碎纸机)
- c语言练习题——递归和栈编程训练
- [算法基础]几个关于递归算法的练习题
- 练习题
- 练习题
- 练习题
- 练习题
- 九大内置对象及四大作用域
- boost asio
- Ubuntu下配置Python进行数据处理的环境
- 连接动态链接库时找不到链接库的解决办法
- 开通
- 递归练习题
- Cocos2d-js3.3虚拟摇杆的实现
- PYTHON正成为中国互联网+的核心技术驱动力
- IOS 将时间字符串转换为时间戳
- C++内置函数(inline function)概念
- Xcode7请求报错AppTransportSecurityhasblocked a cleartext HTTP resource load since it is insecure
- 图片渐进显示的概念以及相关知识
- C# 访问win7 64bit 无安装access的mdb文件出现未在本地计算机上注册“Microsoft.Jet.OLEDB.4.0”
- 矩阵链乘法 有多少种加括号方案