回溯法解决百度、迅雷、中兴等类似笔试题

来源:互联网 发布:oracle查询唯一数据 编辑:程序博客网 时间:2024/05/22 13:17

本文章主要介绍了循环里嵌入递归的应用:以百度、迅雷、中兴等相关笔试题举例子

百度2011年:求一个全排列函数:
p([1,2,3])输出:
[123][132][213][231][321][323]
求一个组合函数
p([1,2,3])输出:
[1][2][3][1,2][2,3][1,3][1,2,3]

这两问可以用伪代码。

求1-N所有的排列:

   

 void fun(int num[],int count,int maxcount)    {        if(count == maxcount)        {            for(int i = 0;i < maxcount;++i)                                                                        System.out.print(num[i]+" ");            System.out.println();            return;        }        int t;        for(int i = count;i < maxcount;++i)        {            t = num[i];            num[i] = num[count];            num[count] = t;            fun(num,count + 1,maxcount);                                                                                                         num[count] = num[i];            num[i] = t;        }    }     publicstaticvoid main(String[] args) {       int[] a = {1,2,3};       new ManProplem().fun(a, 0, a.length);    }


/*

1 2 3

1 3 2

2 1 3

2 3 1

3 2 1

3 1 2

 */

 

求1-N的所有组合:

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class ZhuHe {    /*求钱N个数的组合*/    public void fun(int[] a, int count) {       if (count <= a.length) {           for (int i = 0; i < count; i++) {              System.out.print(a[i] +" ");           }           System.out.println();                         for (int i = count==0?1:(a[count - 1] + 1); i <= a.length; i++) {                  a[count] = i;                  fun(a, count + 1);              }       }    }    public static void main(String[] args)throws NumberFormatException, IOException {       System.out.print("请输入N:");       BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));       int N=Integer.parseInt(buf.readLine());       int[] a =new int[N];       System.out.print("所有的组合如下所示");       new ZhuHe().fun(a, 0);    }}


/*

请输入N3

所有的组合如下所示

1

1 2

1 2 3

1 3

2

2 3

3

*/

 

迅雷2011年:某人上楼梯可以迈123个台阶,编程实现某人上N个台阶的所有可能走法?

package com.jtlyuan.pailie;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;publicclass Pailie {    publicvoid fun(int[] a,int[] num,int count,int N){       int all = 0;       for(int i = 0;i<count;i++){           all+=a[i];       }       if(all>N){           return ;       }       if(all==N){           for(int j = 0;j<count;j++){              System.out.print(a[j]+" ");           }           System.out.println();           return;       }       for(int k = 0;k<3;k++){           a[count]=num[k];           fun(a,num,count+1,N);       }    }    publicstaticvoid main(String[] args)throws NumberFormatException, IOException {                         System.out.print("请输入N:");       BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));       int N=Integer.parseInt(buf.readLine());       int[] a =newint[N];       int[] num = {1,2,3};       new Pailie().fun(a, num, 0,N);    }}


/*

 *请输入N5

1 1 1 1 1

1 1 1 2

1 1 2 1

1 1 3

1 2 1 1

1 2 2

1 3 1

2 1 1 1

2 1 2

2 2 1

2 3

3 1 1

3 2

 **/

 

2010年中兴面试题
编程求解:
输入两个整数 n 和 m,从数列1,2,3.......n 中随意取几个数,
使其和等于 m ,要求将其中所有的可能组合列出来

import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;public class AllPossible { public void findAll(int[] res, int count, int m, int n) {  int all = 0;  for (int i = 0; i < count; i++) {   all += res[i];  }  if (all > m) {   return;  }  if (all == m) {   for (int i = 0; i < count; i++) {    System.out.print(res[i] + " ");   }   System.out.println();   return;  }   for (int i = count==0?1:(res[count - 1] + 1); i <= n; i++) {    res[count] = i;    findAll(res, count + 1, m, n);   } } public static void main(String[] args) throws NumberFormatException,   IOException {  BufferedReader buf = new BufferedReader(    new InputStreamReader(System.in));  System.out.print("请输入M:");  int m = Integer.parseInt(buf.readLine());  System.out.print("请输入M:");  int n = Integer.parseInt(buf.readLine());  int[] res = new int[m];  new AllPossible().findAll(res, 0, m, n); }}


/*

 * 请输入M:10

请输入M:8

1 2 3 4

1 2 7

1 3 6

1 4 5

2 3 5

2 8

3 7

4 6

 */

 

 

编程之美:电话号码对应英语单词?

   

void recur(int[]number,int[] aswer,int index,int n,int[] total,char[][] c){       if(index==n){           for(int i =0;i<n;i++){              System.out.print(c[number[i]][aswer[i]]);           }           System.out.println();           return;       }       for(aswer[index]=0;aswer[index]<total[number[index]];aswer[index]++){           recur(number,aswer,index+1,n,total,c);       }    }    publicstaticvoid main(String[] args) {       char[][] c = {{'$','$','$','$','$','$','$'},{'$','$','$','$','$','$'},{'A','B','C','$','$','$'},{'D','E','F','$','$','$'},{'G','H','I'},{'J','K','L'},{'M','N','O'},{'P','Q','R','S'},{'T','U','V'},{'W','X','Y','Z'}};       int total[]={0,0,3,3,3,3,3,4,3,4};       int number[]={4,3,5};       int aswer[]= {0,0,0,0,0,0,0,0,0,0,0};       new PhoneNumber().recur(number,aswer,0,number.length,total,c);;


 

现在给大家出两个相关的扩展问题:

27.跳台阶问题
题目:一个台阶总共有n级,如果一次可以跳1级,也可以跳2级。
求总共有多少总跳法,并分析算法的时间复杂度。

这道题最近经常出现,包括MicroStrategy等比较重视算法的公司都
曾先后选用过个这道题作为面试题或者笔试题。

 

2.n个骰子的点数。
把n个骰子扔在地上,所有骰子朝上一面的点数之和为S。
输入n,打印出S的所有可能的值出现的概率。

原创粉丝点击