算法:【一列数的规则如下: 1、1、2、3、5、8、13、21、34 ,求第30位数是多少, 用递归算法实现。(C#语言)】

  /// <summary>
        
/// 一列数的规则如下: 1、1、2、3、5、8、13、21、34   求第30位数是多少, 用递归算法实现。(C#语言)
        
/// </summary>
        
/// <param name="pos"></param>
        
/// <returns></returns>

        public int GetNumberAtPos(int pos)
        
{
            
if(pos==0||pos==1)
            
{
                
return 1;
            }

            
int res = GetNumberAtPos(pos - 1+ GetNumberAtPos(pos - 2);
            
return res;
        }
作者:jillzhang
出处:http://jillzhang.cnblogs.com/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
posted @ 2007-09-05 09:03 Robin Zhang 阅读(4348) 评论(26)  编辑 收藏 网摘 所属分类: 算法

  回复  引用    
#1楼2007-09-05 09:09 | Zeus2[未注册用户]
递归算法有个问题。
从n-2到3都算了2次。

  回复  引用  查看    
#2楼[楼主]2007-09-05 09:28 | jillzhang      
@Zeus2
有什么解决办法么?

  回复  引用  查看    
#3楼2007-09-05 09:31 | 没剑      
应该要从第三位开始才递归吧
Private Function getPos(ByVal pos As Integer) As Integer
If pos <= 2 Then Return 1
Return getPos(pos - 2) + getPos(pos - 1)
End Function

  回复  引用  查看    
#4楼[楼主]2007-09-05 09:53 | jillzhang      
@没剑
我的也是从第三位开始的咧

  回复  引用    
#5楼2007-09-05 10:08 | 小7[未注册用户]
那道面试题......
  回复  引用  查看    
#6楼2007-09-05 10:17 | D.Oliver      
不用递归不是更好吗?
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Test
{
public class Class1
{
private ArrayList list = new ArrayList();

public Class1()
{
}

public Class1(int num)
:base()
{
int i;

for (i = 1; i <= num; i++)
{
list.Add(Calculation(i));
}
}

private int Calculation(int num)
{
if (num == 1||num == 2)
return 1;
else
return Convert.ToInt32(list[num - 2]) + Convert.ToInt32(list[num - 3]);
}

public int Calculation()
{
return Convert.ToInt32(list[list.Count-1]);
}
}

public class test
{
public static void Main()
{
int j;
int num;
for( j=1;j<100;j++)
{
Console.WriteLine("你要计算第多少位:");
string readstr;
readstr=Console.ReadLine();
if (!string.IsNullOrEmpty(readstr))
{
if (int.TryParse(readstr, out num))
{
if (num < 1)
continue;
else
{
Class1 c1 = new Class1(num);
Console.WriteLine(c1.Calculation());
}
}
else
{
continue;
}
}
else
{
break;
}
}
}
}
}

  回复  引用  查看    
#7楼2007-09-05 11:56 | 没剑      
@jillzhang
貌似你的位置还有0的...

  回复  引用  查看    
#8楼2007-09-05 11:58 | 没剑      
老大真牛

for (i = 1; i <= num; i++)
{
list.Add(Calculation(i));
}
}


return Convert.ToInt32(list[num - 2]) + Convert.ToInt32(list[num - 3]);

  回复  引用  查看    
#9楼[楼主]2007-09-05 12:05 | jillzhang      
其实这个方法最大的问题在哪?
在于没有验证pos的正负,这样的一个函数是很容易出错的,也是一个正确的函数,在正确使用的情况下是对的,但特殊情况下就异常了,不知道大家看出来没有

  回复  引用  查看    
#10楼2007-09-05 12:11 | 没剑      
Private Function getPos(ByVal pos As Integer) As Integer
If pos <= 2 Then Return 1
Return getPos(pos - 2) + getPos(pos - 1)
End Function
我的完成没有这个问题 

  回复  引用  查看    
#11楼2007-09-30 08:56 | 麒麟.NET      
Fibonacci数列啊
  回复  引用  查看    
#12楼2007-11-06 10:42 | 1-2-3      
好像有一种用公式来计算的方法,公式好像挺复杂的,好像在网上见过一次。
  回复  引用    
#13楼2007-11-25 17:27 | possible[未注册用户]
public int GetNumber(int index) // index从1开始
{
int a1=1,
int a2=1,
int temp;
int count = 3
while(index >= count)
{
temp = a2;
a2 = a1+a2
a1 = temp
count++
}
return a2;
}

  回复  引用    
#14楼2007-12-21 14:50 | 小耿[未注册用户]
using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication3
{
class A
{
public int GetNumberAtPos(int pos)
{
if (pos == 0 || pos == 1)
{
return 1;
}
int res = GetNumberAtPos(pos - 1) + GetNumberAtPos(pos - 2);
return res;
}
}
class Program
{

static void Main(string[] args)
{
A n = new A();

Console.WriteLine(n.GetNumberAtPos(29));
Console.ReadLine();
//int i=1;
//while (i < 4)
//{

//}

}
}
}

  回复  引用    
#15楼2007-12-26 22:15 | NetCare[未注册用户]
int a1=1,a2=1,returnValue;
for(int i=2;i<num;i++)
{
returnValue=a2;
a2+=a1;
a1=returnValue;
}
return a1;

  回复  引用    
#16楼2008-02-05 14:37 | imetgong[未注册用户]
public int getNumber(int pos) {
if (pos < 1) {
return -1;
}

if (pos < 3) {
return 1;
}

while (pos > 0) {
return getNumber(pos - 2) + getNumber(pos - 1);
}

return -1;
}

  回复  引用  查看    
#17楼2008-07-29 16:22 | shawnliu      
代码效率太差了把
只需要用两个变量保存前两位不就行了 再加一个当前数值,
最后更新一下变量


  回复  引用  查看    
#18楼2008-07-31 14:23 | 丛林之王      
楼主这个貌似 应该这样
public int GetNumberAtPos(int pos)
{
if (pos == 0 ||pos == -1 )
{
return 0;
}
if ( pos == 1)
{
return 1;
}
int res = GetNumberAtPos(pos - 1) + GetNumberAtPos(pos - 2);
return res;
}

  回复  引用    
#19楼2008-11-25 08:23 | 哲思[未注册用户]
支持13楼的
using System;
using System.Collections.Generic;
using System.Text;

namespace 算法设计_一列数
{
class Program
{
static void Main(string[] args)
{

DateTime time2 = DateTime.Now;
Console.WriteLine(time2.ToString());
int number2 = GetNumber(30);
DateTime time3 = DateTime.Now;
Console.WriteLine(time3.ToString());
TimeSpan t2 = time3.Subtract(time2);
Console.WriteLine(t2.ToString());
Console.WriteLine(number2);
DateTime time = DateTime.Now;
Console.WriteLine(time.ToString());
int number = getNumber(30);
DateTime time1 = DateTime.Now;
Console.WriteLine(time1.ToString());
TimeSpan t1 = time1.Subtract(time);
Console.WriteLine(t1.ToString());
Console.WriteLine(number);

}


public static int getNumber(int pos)
{
if (pos == 0 || pos == 1)
{
return 1;

}
int s = getNumber(pos - 1) + getNumber(pos-2);
return s;

}
public static int GetNumber(int index) // index从1开始
{
int a1=1;
int a2=1;
int temp;
int count = 2;
while(index >= count)
{
temp = a2;
a2 = a1+a2;
a1 = temp;
count++;
}
return a2;
}

}
}
执行以后就可以看出差别了
数值的索引越往后差别越明显!

  回复  引用    
#20楼2009-03-16 10:09 | test111111111111111[未注册用户]
int fwdadd(int i)
{
int s,m,n,flag;
if(i<3)
return 1;
else
{
m=1;n=1;flag=0;s=1;
for(int k=3;k<=i;k++)
{
flag=1-flag;
if(flag)
m=m+n;
else
n=n+m;

}
if(flag)
return m;
else
return n;
}
}

  回复  引用  查看    
#21楼2009-05-26 13:44 | Funeral      
不用递归也能很简单的

int fibonacci = 0;
for (int i = 0, j = 0, k = 1, fib = 1; i < 30; fib = j + k, j = k, k = fib, i++)
{
fibonacci = fib;
}
Console.WriteLine(fibonacci);
Console.Read();




发表评论

昵称: [登录] [注册]

主页:

邮箱:(仅博主可见)

评论内容:

  登录  注册

[使用Ctrl+Enter键快速提交评论]

0 882342




相关文章:

相关链接: