【问题标题】:Adding a value to the Array inside a while loop在while循环中向数组添加值
【发布时间】:2014-05-27 23:12:29
【问题描述】:

我正在制作一个运行斐波那契数列的程序。我创建了 2 个数组。

  1. 第一个数组仅包含 0, 1(数组名称:- int[] arr)

  2. 第二个数组保存其他值,例如:1、2、3、5....等(数组名称:- int[] numbers)

我正在使用 while 循环来获取 febonacci 系列并将其存储在称为 int[] 数字的第二个数组中。

使用 while 循环获取值后,我使用

连接两个数组
int[] final = arr.Concat(number).ToArray();

最后,我使用 foreach 循环将 febonacci 系列添加到列表框中。

我遇到的问题是,我无法连接两个数组。我试图在 while 循环的顶部分配数字数组。这样数字变量就可以在 while 循环之外访问。但我遇到了一个错误。

请看下面的代码:

private void button1_Click(object sender, EventArgs e)
{
    int x = 0;
    int y = 1;
    int z = 0;

    if (!String.IsNullOrEmpty(q1input.Text))
    {
        int value;
        if (int.TryParse(q1input.Text, out value))
        {
            int[] arr = {x, y };

            while (z < value)
            {
                z = x + y;
                int[] number = {z};
                x = y;
                y = z;
            }
            int[] final = arr.Concat(number).ToArray();
            foreach (int num in final)
            {
                q2listbox.Items.Add(num);
            }
        }
        else
        {
            MessageBox.Show("It is not a numeric value");
        }
    }
    else
    {
        MessageBox.Show("Invalid Input");
    }
}

【问题讨论】:

  • 这是你的作业吗?
  • 你能解释一下javac++问题的标签吗?
  • 抱歉打错了标签。我是stackoverflow的新手。我想如果标记更多,我会很快得到帮助。
  • 错了,如果你标记正确,你会得到帮助
  • 好的。这不是家庭作业。只是通过一些互联网任务练习 C#。

标签: c# fibonacci


【解决方案1】:
                List<int> number = new List<int>();

                while (z < value)
                {
                    z = x + y;
                    number.Add(z);
                    x = y;
                    y = z;

                }
                int[] final = arr.Concat(number).ToArray();

【讨论】:

  • 任何解释都会对 OP 有所帮助。所以不是关于“最快的答案赢得价格”
【解决方案2】:

如果您将您的关注点分开可能会有所帮助:计算斐波那契数列应该与您的用户界面代码分开。

您的部分问题是您正在使用 C# 中的数组(固定长度)构建长度可调的东西。 List&lt;T&gt; 是一种更适合您的数据结构。尽管名称具有误导性,但它是一个长度可调的数组,而不是计算机科学意义上的实际列表。

生成斐波那契数列并不像您制作的那么复杂。这个实现:

public int[] FibonacciSequence( int x1 , int x2 , int upperBound )
{
  if ( x1 < 0 ) throw new ArgumentException("x1 can't be negative") ;
  if ( x2 < 0 ) throw new ArgumentException("x2 can't be negative") ;
  if ( x1 == 0 && x2 == 0 ) throw new ArgumentException("x1 and x2 can't both be zero.") ;

  List<int> values = new List<int>() ; // use List<int>, since we don't know the length in advance

  values.Add(x1) ; // the first 2 elements are given
  values.Add(x2) ;

  // the remaining elements are computed by summing the previous two elements and shifting.
  for ( int x = x1+x2 ; x > 0 && x < upperBound ; x = x1+x2 )
  {

     // add the new value to the list of values
    values.Add(x) ;

    x1 = x2 ; // x1 receives x2 (with the current x1 shifting off into oblivion
    x2 = x  ; // x2 receives x

  }

  int[] sequence = values.ToArray() ;
  return sequence ;
}

没有规则,只是约定,斐波那契数列以 [0,1] 或 [1,1] 开头。然后,您可以使用所需的种子调用此函数,因此:

int[] fibonacci = FibonacciSequence(1,1,int.MaxValue) ;

斐波那契数列最酷的地方在于,无论种子值如何,在数列中越往前走,任何两个相邻值的比率都会收敛到 phi,即黄金均值。

更简单的是使用一些 LINQ 的功能和魔法。使用它,您的斐波那契数列变得更加简单:

public IEnumerable<int> FibinacciSequence( int x1 , int x2 )
{
  yield return x1 ;
  yield return x2 ;

  for ( int x = x1+x2 ; x > 0 && x < int.MaxValue ; x = x1+x2 )
  {
    yield return x ;
    x1 = x2 ;
    x2 = x  ;
  }
}

它的用法变成了这样:

int[] sequence = FibonacciSequence(1,1)
                 .TakeWhile( x => x < upperBound )
                 .ToArray()
                 ;

您甚至可以跳过 'ToArray() 位并简单地说类似

foreach ( int value in FibonacciSequence(1,1).TakeWhile( x => x < upperBound ) )
{
  q2listbox.Items.Add( value ) ;
}

当您将每个值添加到列表框中时,它将以惰性方式评估序列。

【讨论】:

  • 天哪。 int.MaxValue...不要在家里尝试这个;-)
  • x 应该是 long 如果您要允许 upperBound = int.MaxValue
  • @DourHighArch,真的。但在这种情况下是一个细节
猜你喜欢
  • 2018-06-30
  • 2019-09-20
  • 2011-07-08
  • 1970-01-01
  • 2012-04-15
  • 1970-01-01
  • 2012-10-02
  • 1970-01-01
相关资源
最近更新 更多