点滴积累【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         }

 

转载于:https://www.cnblogs.com/xinchun/archive/2013/04/11/3015438.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值