• 递归算法


     1 using System;
     2 using System.Windows.Forms;
     3 
     4 namespace DemoCore
     5 {
     6     public partial class Recursion : Form
     7     {
     8         public Recursion()
     9         {
    10             InitializeComponent();
    11         }
    12 
    13         private void button1_Click(object sender, EventArgs e)
    14         {
    15             this.textBox2.Text = ComputePeach(Convert.ToInt32(this.textBox1.Text)).ToString();
    16             ComputPeach();
    17         }
    18         /// <summary>
    19         /// 1*1+2*2+3*3+4*4+...+n*n,求n个相加后的和
    20         /// </summary>
    21         private int ComputeAdd(int i)
    22         {
    23             if (i <= 2 && i > 0)
    24             {
    25                 return 1+ i * i;
    26             }
    27             else
    28             {
    29                 return ComputeAdd(i - 1) + i * i;
    30             }
    31 
    32         }
    33 
    34         /**
    35          *  趣味问题——年龄。有5个人坐在一起,
    36          *  问第五个人多少岁?他说比第4个人大2岁。
    37          *  问第4个人岁数,他说比第3个人大2岁。
    38          *  问第三个人,又说比第2人大两岁。
    39          *  问第2个人,说比第一个人大两岁。
    40          *  最后问第一个人,他说是10岁。
    41          *  请问第五个人多大?用递归算法实现。
    42          *  
    43          * */
    44         private int ComputeAge(int i)
    45         {
    46             if (i == 1)
    47             {
    48                 return 10;
    49             }
    50             else
    51             {
    52                 return ComputeAge(i-1) + 2;
    53             }
    54         }
    55     }
    56 }
  • 相关阅读:
    模拟展示动态按钮
    模拟界面请求到web服务器
    bean的生命周期
    structs2的action实现方式
    annotation中的Autowired
    华为笔试题练习
    开发工具
    [转]Linux双向链表的知识
    【转】 嵌入式C语言编程中Inline函数的应用
    打印格式化printf
  • 原文地址:https://www.cnblogs.com/Lijq/p/4463175.html
Copyright © 2020-2023  润新知