生成斐波纳契序列并为其创建子集 [英] Generating fibonacci sequence and creating subsets for it
问题描述
我正在尝试生成一个通过从数组[0,1]开始计算的Fibonacci序列,并且通过在它之前添加两个数字来计算每个后续数字。 //例如0,1,[0 + 1 =] 1,[1 + 1 =] 2,[1 + 2 =] 3,[2 + 3 =] 5,依此类推。
我试图实现的两种方法是下面但是我很难停留在生成子集(GenerateSubset(params))。任何帮助都会非常明显。
我尝试过:
public IEnumerable< long>生成()
{
int i,count,f1 = 0,f2 = 1,f3 = 0;
Console.Write(输入限制:);
count = int.Parse(Console.ReadLine());
Console.WriteLine(f1);
Console.WriteLine(f2);
for(i = 0; i< = count; i ++)
{
f3 = f1 + f2;
Console.WriteLine(f3);
f1 = f2;
f2 = f3;
}
Console.ReadLine();
}
public Task< IEnumerable< long>> GenerateSubset(int fromIndex,int toIndex)
{
抛出新的NotImplementedException();
}
生成Fibonacci系列的c ++程序简化编程 [ ^ ] 。
完整的解决方案,如果这是你想要的
使用系统;
使用 System.Collections.Generic;
使用 System.Linq;
使用 System.Text;
使用 System.Threading.Tasks;
命名空间 ConsoleApplication
{
class Program
{
static IEnumerable< long> fibs = Enumerable.Empty< long>();
static int count,fromIndex,toIndex;
静态 void Main( string [] args)
{
while ( true )
{
Generate();
Console.Write( string .Format( 输入0到{0} \ n,count)之间的索引范围);
Console.Write( 输入第一个索引:);
fromIndex = int .Parse(Console.ReadLine());
Console.Write( 输入最后一个索引:);
toIndex = int .Parse(Console.ReadLine());
GenerateSubset(fromIndex,toIndex);
}
}
public static void 生成()
{
int i;
long f1 = 0 ,f2 = 1 ,f3 = 0 ;
Console.Write( 输入限制:);
count = int .Parse(Console.ReadLine());
// Console.WriteLine(f1);
// Console.WriteLine(f2);
fibs = fibs.Concat( new [] {f2});
Console.Write( 完整序列:\ n);
Console.WriteLine(f2);
for (i = 0 ; i < ; count; i ++)
{
f3 = f1 + f2;
fibs = fibs.Concat( new [] {f3});
Console.WriteLine(f3);
f1 = f2;
f2 = f3;
}
}
静态 public void GenerateSubset( int fromIndex, int toIndex)
{
for ( int i = fromIndex; i < = toIndex; i ++)
{
Console.WriteLine(fibs.ElementAt(i));
}
Console.WriteLine( \ n);
}
}
}
I am trying to generate a Fibonacci sequence computed by starting with the array [ 0, 1 ] and each subsequent number is computed by adding the two numbers before it. // E.g. 0, 1, [0 + 1 =] 1, [1 + 1 =] 2, [1 + 2 =] 3, [2 + 3 =] 5, and so on.
Two methods I am trying to implement are below however i am badly stuck in generating a subsets(GenerateSubset(params)). Any help would be really appreciable.
What I have tried:
public IEnumerable<long> Generate()
{
int i, count, f1 = 0, f2 = 1, f3 = 0;
Console.Write("Enter the Limit : ");
count = int.Parse(Console.ReadLine());
Console.WriteLine(f1);
Console.WriteLine(f2);
for (i = 0; i <= count; i++)
{
f3 = f1 + f2;
Console.WriteLine(f3);
f1 = f2;
f2 = f3;
}
Console.ReadLine();
}
public Task<IEnumerable<long>> GenerateSubset(int fromIndex, int toIndex)
{
throw new NotImplementedException();
}
c++ program to generate Fibonacci series | Programming Simplified[^].
Full solution, if this is what you want
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication { class Program { static IEnumerable<long> fibs = Enumerable.Empty<long>(); static int count, fromIndex, toIndex; static void Main(string[] args) { while (true) { Generate(); Console.Write(string.Format("Enter index range between 0 and {0}\n", count)); Console.Write("Enter first index : "); fromIndex = int.Parse(Console.ReadLine()); Console.Write("Enter last index : "); toIndex = int.Parse(Console.ReadLine()); GenerateSubset(fromIndex, toIndex); } } public static void Generate() { int i; long f1 = 0, f2 = 1, f3 = 0; Console.Write("Enter the Limit : "); count = int.Parse(Console.ReadLine()); //Console.WriteLine(f1); //Console.WriteLine(f2); fibs = fibs.Concat(new[] { f2 }); Console.Write("Full sequence :\n"); Console.WriteLine(f2); for (i = 0; i < count; i++) { f3 = f1 + f2; fibs = fibs.Concat(new[] { f3 }); Console.WriteLine(f3); f1 = f2; f2 = f3; } } static public void GenerateSubset(int fromIndex, int toIndex) { for (int i = fromIndex; i <= toIndex; i++) { Console.WriteLine(fibs.ElementAt(i)); } Console.WriteLine("\n"); } } }
Program screenshot: http://imgur.com/ogER8oQ
这篇关于生成斐波纳契序列并为其创建子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!