zoukankan      html  css  js  c++  java
  • 点滴积累【C#】1、1、2、3、5、8、13、21....计算第30位(递归)!

    Question:用递归计算1、1、2、3、5、8、13、21....第30位!

    效果:

    代码:

     1 protected void Button1_Click(object sender, EventArgs e)
     2         {
     3             if (tb1.Text != "" && tb1.Text != null)
     4             {
     5                 if (!Isnum(tb1.Text))
     6                 {
     7                     Response.Write("<script type='text/javascript'>alert('请输入数字');</script>");
     8                 }
     9                 else
    10                 {
    11                     int a = Convert.ToInt32(tb1.Text);
    12                     tb2.Text = Convert.ToString(Foo(a));
    13                 }
    14             }
    15             else
    16             {
    17                 Response.Write("<script type='text/javascript'>alert('不能为空');</script>");
    18             }
    19         }
    20         public static int Foo(int i)
    21         {
    22             if (i < 0)
    23             {
    24                 return 0;
    25             }
    26             else if (i > 0 && i <= 2)
    27             {
    28                 return 1;
    29             }
    30             else
    31             {
    32                 return Foo(i - 1) + Foo(i - 2);
    33             }
    34         }
    35         public static bool Isnum(string s)
    36         {
    37             string pattern = @"^\d*$";
    38             return Regex.IsMatch(s, pattern);
    39         }
  • 相关阅读:
    并查集基础练习
    HDU1232——畅通工程
    二分答案——划分数列
    二分答案——收入计划
    动态规划练习题(2)
    动态规划程序设计2
    动态规划练习题(1)
    0/1背包
    P5024 保卫王国[倍增+dp]
    UVA11424 GCD
  • 原文地址:https://www.cnblogs.com/xinchun/p/3015438.html
Copyright © 2011-2022 走看看