A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© lpz869 中级黑马   /  2014-5-28 23:02  /  1296 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 lpz869 于 2014-5-29 10:54 编辑

为什么在用到递归的时候总是没有头绪

点评

递归是一个稍微难的算法思想。慢慢理解。  发表于 2014-5-29 18:48

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

6 个回复

倒序浏览
你要明白什么情况下用递归,递归执行的条件,好处,特点
回复 使用道具 举报
递归.我是这样认为的.只要想清楚第一层的 思路.就可以了
回复 使用道具 举报
递归就是把大问题转化为小问题,然后解决小问题。
你在考虑一个问题的时候要这么想:
这个问题是什么?能够分解为更简单的 有规律的小问题吗?
如果能
那么好,现在编写一个方法解决小问题,
解决完小问题你要考虑,让方法跳出。
以下是我练习递归时 自己做得题 在这里我要感谢一下袁晓俊同学,是他出的一道题让我更加深刻的理解了递归。
袁晓俊同学出的题:
写一个程序,要求用户任意输入N个字符,然后输出这些字符组合出现的可能
如:输入ab1
可能出现的字符串:ab1 a1b ba1 b1a 1ab 1ba
我的做法:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;

  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.WriteLine("请输入任意字符!");
  13.             string str;
  14.         LuRuZiFu:
  15.             str = Console.ReadLine();
  16.             if (str == string.Empty)
  17.             {
  18.                 Console.WriteLine("不能输入空字符串,请重新输入!");
  19.                 goto LuRuZiFu;
  20.             }
  21.             for (int i = 0; i < str.Length; i++)
  22.             {
  23.                 char temp = str[i];
  24.                 int suoyin = str.IndexOf(temp);
  25.                 if (suoyin != i && suoyin != -1)
  26.                 {
  27.                     Console.WriteLine("字符串有重复的字符,请重新输入!");
  28.                     goto LuRuZiFu;

  29.                 }
  30.             }
  31.             Console.Write("可能出现的字符!");
  32.             PaiLieZuHe(str.ToCharArray(), 0, str.Length);
  33.             Console.ReadKey();
  34.         }
  35.         static void JiaoHuan(ref char a, ref char b)
  36.         {
  37.             char temp = a;
  38.             a = b;
  39.             b = temp;
  40.         }
  41.         static void PaiLieZuHe(char[] jiaohuanwenben, int kaishi, int jieshu)
  42.         {
  43.             //交换到最后一位
  44.             if (kaishi == jieshu - 1)
  45.             {
  46.                 Console.Write(new string(jiaohuanwenben) + " ");
  47.             }
  48.             else
  49.             {
  50.                 for (int i = kaishi; i < jieshu; i++)
  51.                 {
  52.                     JiaoHuan(ref jiaohuanwenben[i], ref jiaohuanwenben[kaishi]);
  53.                     PaiLieZuHe(jiaohuanwenben, kaishi + 1, jieshu);
  54.                     JiaoHuan(ref jiaohuanwenben[i], ref jiaohuanwenben[kaishi]);
  55.                 }
  56.             }
  57.         }
  58.     }
  59. }
复制代码
网上找的题

1.把一个整数按n(2<=n<=16)进制表示出来,并保存在给定字符串中。比如121用二进制表示得到结果为:“1111001”。
我的做法
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;

  6. namespace 一个数字的所有进制显示用递归表示出来
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             List<List<char>> suoyoujinzhi = new List<List<char>>();
  13.             Console.WriteLine("请输入数字");
  14.             int tempint = Convert.ToInt32(Console.ReadLine());
  15.             SuoYouJinZhi(tempint, 2, 16, suoyoujinzhi);
  16.             foreach (var item in suoyoujinzhi)
  17.             {
  18.                 string str = new string(item.ToArray());
  19.                 Console.WriteLine(str);
  20.             }
  21.             Console.ReadKey();
  22.         }
  23.         static char PiPeiZiFu(int i)
  24.         {
  25.             switch (i)
  26.             {
  27.                 case 10:
  28.                     return 'A';
  29.                 case 11:
  30.                     return 'B';
  31.                 case 12:
  32.                     return 'C';
  33.                 case 13:
  34.                     return 'D';
  35.                 case 14:
  36.                     return 'E';
  37.                 case 15:
  38.                     return 'F';
  39.                 default:
  40.                     return Convert.ToChar(i.ToString());
  41.             }
  42.         }
  43.         static void SuoYouJinZhi(int shuzi, int kaishi, int jieshu, List<List<char>> suoyoujinzhi)
  44.         {

  45.             if (kaishi == jieshu + 1)
  46.             {
  47.                 return;
  48.             }
  49.             else
  50.             {
  51.                 //保存输入数字的值
  52.                 int tempint = shuzi;
  53.                 //创建一个字符串用来保存所得到的N进制结果
  54.                 List<char> temp = new List<char>();
  55.                 while (shuzi >= kaishi)
  56.                 {
  57.                     //把余数加入到字符串开头
  58.                     temp.Insert(0, PiPeiZiFu(shuzi % kaishi));
  59.                     //更新原数字。
  60.                     shuzi /= kaishi;
  61.                 }
  62.                 //如果剩余的数字不是0那就把它加到开头
  63.                 if (shuzi != 0)
  64.                 {
  65.                     temp.Insert(0, PiPeiZiFu(shuzi));
  66.                 }
  67.                 //在开头加上是多少进制。
  68.                 temp.InsertRange(0, string.Format("{0}进制结果:", kaishi).ToCharArray());
  69.                 //把第N进制的结果保存到所有进制里
  70.                 suoyoujinzhi.Add(temp);
  71.                 // 进行下一个进制的计算
  72.                 SuoYouJinZhi(tempint, ++kaishi, jieshu, suoyoujinzhi);
  73.             }
  74.         }
  75.     }
  76. }
复制代码





评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报
还有几道简单的,你也可以看看
求数组中最大数
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;

  6. namespace 求数组中最大数
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             int[] shuzu = new int[] { 10, 2, 5, 611, 111, 45, 123 };
  13.             int weizhi = ZuiDaShuWeiZhi(shuzu, 0, 0);
  14.             Console.WriteLine("最大数为{0}", shuzu[weizhi]);
  15.             Console.ReadKey();
  16.         }
  17.         static int ZuiDaShuWeiZhi(int[] a, int weizhi, int i)
  18.         {
  19.             if (i == a.Length)
  20.             {
  21.                 return weizhi;
  22.             }
  23.             if (a[i] > a[weizhi])
  24.             {
  25.                 return ZuiDaShuWeiZhi(a, i, ++i);
  26.             }
  27.             return ZuiDaShuWeiZhi(a, weizhi, ++i);
  28.         }
  29.     }
  30. }
复制代码

1,2,3....n 求n
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;

  6. namespace 递归
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.WriteLine("1,2,3....n");
  13.             Console.WriteLine("n为?");
  14.             string str = Console.ReadLine();
  15.             Console.WriteLine(DiGui(Convert.ToInt32(str) - 1));
  16.             Console.ReadKey();
  17.         }
  18.         static int DiGui(int n)
  19.         {
  20.             if (n == 0)
  21.             {
  22.                 return 1;
  23.             }
  24.             if (n == 1)
  25.             {
  26.                 return 2;
  27.             }
  28.             return DiGui(n - 2) + DiGui(n - 1);

  29.         }
  30.     }
  31. }
复制代码

求n个整数的乘积
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;

  6. namespace 求n个整数的乘积
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             Console.WriteLine(ChengJi(6));
  13.             Console.ReadKey();
  14.         }
  15.         static int ChengJi(int i)
  16.         {
  17.             if (i == 1)
  18.             {
  19.                 return 1;
  20.             }
  21.             return i * ChengJi(--i);
  22.         }
  23.     }
  24. }
复制代码

版主同学,我明天就面试了,技术分还不够,看在我这么辛苦的份上来点分吧

点评

恩 ,非常详细,,,代码借鉴了,版主帮加分  发表于 2014-5-29 22:04

评分

参与人数 1技术分 +1 收起 理由
czwanglei + 1

查看全部评分

回复 使用道具 举报 1 0
您需要登录后才可以回帖 登录 | 加入黑马