首先我们要计算一组数的第多少个数,我们就要先将他的规律看出来,我们看这组数1,1,2,3,5我们仔细看一下就能看出来,他的规律就是前一个加后一个数=第三个数,我们看出规律后就可以子c#里面来编写代码来计算这个第30个数了
using System;
namespace guil_lx
{
class Program
{
public long js(long f)
{
long a = 1;
long b = 1;
long c = 2;
long d = 0;
if(f==1)//前三个数已经定义就直接输出即可
{
Console.WriteLine("第{0}个数为{1}", f, a);
d = a;
//Console.ReadLine();
}
if(f==2)
{
Console.WriteLine("第{0}个数为{1}", f, b);
d = b;
//Console.ReadLine();
}
if(f==3)
{
Console.WriteLine("第{0}个数为{1}", f, c);
d = c;
//Console.ReadLine();
}
if (f > 3)
{
long y = f % 3;//用一个数来记录我们要算的这个数是不是3的倍数
long q = f / 3;//用q来记录我们要循环的次数;
if(y==0)//如果我们的要求的这个数是3的倍数
{
q = q - 1;//那么我们的循环次数就减一;
}
for (int i = 0; i < q; i++)
{
a = b + c;//a=3
b = a + c;//b=5
c = a + b;//c=8
//一套3个数的循环
}
if (f % 3 == 1)//用3的倍数来判断我们应该取那个数
{
Console.WriteLine("第{0}个数为{1}", f, a);
d = a;
//Console.ReadLine();
}
if (f % 3 == 2)
{
Console.WriteLine("第{0}个数为{1}", f, b);
d = b;
//Console.ReadLine();
}
if (f % 3 == 0)
{
Console.WriteLine("第{0}个数为{1}", f, c);
d = c;
//Console.ReadLine();
}
}
return d;
}
static void Main(string[] args)
{
Program program = new Program();
while(true)
{
long e = Convert.ToInt64(Console.ReadLine());
program.js(e);
}
}
}
}
上面就是我写的求数的方法。我觉得我写的太麻烦了,应该有简单的多的办法,和不一样的规律但是我暂时就想到这个办法,如有更好的方法或者错误请多指教