根据用户输入显示斐波那契条目的最有效方法
Most efficient method to display fibonacci entries based on user input
根据用户的输入,我将如何正确编写一个算法,在每行有 5 列的 table 中显示斐波那契的每个条目。
在这个程序中,用户将输入一个数字,该数字将代表斐波那契数列中的多少个数字将显示在 table 中。 Fibonacci
视图模型如下:
namespace TestingAndLogging.Models
{
public class Fibonacci
{
public int Input { get; set; }
public List<string> Output { get; set; } = new List<string>();
}
}
我希望 table 为 5 列宽。如果用户输入 11,则前两行将包含前十个数字,最后一行将仅包含一个数字。现在,出于显示目的,我只使用一行并在该行上显示所有数字,因为每次我尝试实现一种算法来显示数字时,任何不能被 5 整除的用户输入都不会 运行正确地通过算法。
<div class="row mt-5">
<div class="offset-1 col-10">
<table class="table table-striped justifiy-content-center" style="background-color: #ADE1FA;">
<thead>
<tr><th colspan="5">Fibonacci Series Results</th></tr>
</thead>
<tBody>
<tr>
@for (int i = 0; i < Model.Output.Count; i++)
{
<td>@Model.Output[i]</td>
}
</tr>
</tBody>
</table>
</div>
</div>
以下是数字的计算方式:
public List<string> CalculateFibonacci(Fibonacci model)
{
List<string> returnList = new List<string>();
int num1 = 0;
int num2 = 1;
int num3;
//Put the first two numbers of the fib series into the array
returnList.Add($"{num1}");
returnList.Add($"{num2}");
if (model.Input == 1)
{
returnList.RemoveAt(1);
}
//Get the subsequent numbers after the first two numbers in the series and put
//them into a List of ints
else if (model.Input > 2)
{
for (int i = 2; i < model.Input; i++)
{
num3 = num1 + num2;
num1 = num2;
num2 = num3;
returnList.Add($"{num3}");
}
}
return returnList;
}
由于您需要多行,每行有多个单元格,因此您需要某种嵌套循环安排。
外循环的每次迭代都会添加一个新行,内循环负责添加该行中的单元格。
注意内层循环的条件必须同时检查col
小于5和计算出的索引row * 5 + col
小于元素总数
<tbody>
@for (int row = 0; row * 5 < Model.Output.Count; row++)
{
<tr>
@for (int col = 0; col < 5 && row * 5 + col < Model.Output.Count; col++)
{
<td>@Model.Output[row * 5 + col]</td>
}
</tr>
}
</tbody>
您可能还想用一个常量替换所有这些 5,因此稍后您只需调整一个值即可更改列数。
根据用户的输入,我将如何正确编写一个算法,在每行有 5 列的 table 中显示斐波那契的每个条目。
在这个程序中,用户将输入一个数字,该数字将代表斐波那契数列中的多少个数字将显示在 table 中。 Fibonacci
视图模型如下:
namespace TestingAndLogging.Models
{
public class Fibonacci
{
public int Input { get; set; }
public List<string> Output { get; set; } = new List<string>();
}
}
我希望 table 为 5 列宽。如果用户输入 11,则前两行将包含前十个数字,最后一行将仅包含一个数字。现在,出于显示目的,我只使用一行并在该行上显示所有数字,因为每次我尝试实现一种算法来显示数字时,任何不能被 5 整除的用户输入都不会 运行正确地通过算法。
<div class="row mt-5">
<div class="offset-1 col-10">
<table class="table table-striped justifiy-content-center" style="background-color: #ADE1FA;">
<thead>
<tr><th colspan="5">Fibonacci Series Results</th></tr>
</thead>
<tBody>
<tr>
@for (int i = 0; i < Model.Output.Count; i++)
{
<td>@Model.Output[i]</td>
}
</tr>
</tBody>
</table>
</div>
</div>
以下是数字的计算方式:
public List<string> CalculateFibonacci(Fibonacci model)
{
List<string> returnList = new List<string>();
int num1 = 0;
int num2 = 1;
int num3;
//Put the first two numbers of the fib series into the array
returnList.Add($"{num1}");
returnList.Add($"{num2}");
if (model.Input == 1)
{
returnList.RemoveAt(1);
}
//Get the subsequent numbers after the first two numbers in the series and put
//them into a List of ints
else if (model.Input > 2)
{
for (int i = 2; i < model.Input; i++)
{
num3 = num1 + num2;
num1 = num2;
num2 = num3;
returnList.Add($"{num3}");
}
}
return returnList;
}
由于您需要多行,每行有多个单元格,因此您需要某种嵌套循环安排。 外循环的每次迭代都会添加一个新行,内循环负责添加该行中的单元格。
注意内层循环的条件必须同时检查col
小于5和计算出的索引row * 5 + col
小于元素总数
<tbody>
@for (int row = 0; row * 5 < Model.Output.Count; row++)
{
<tr>
@for (int col = 0; col < 5 && row * 5 + col < Model.Output.Count; col++)
{
<td>@Model.Output[row * 5 + col]</td>
}
</tr>
}
</tbody>
您可能还想用一个常量替换所有这些 5,因此稍后您只需调整一个值即可更改列数。