我有使用組合方法的編碼。C#如何僅顯示組合的第一個,第二個和第三個答案?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
string input;
int NoDisplay;
decimal goal;
decimal element;
do
{
Console.WriteLine("Please enter the target:");
input = Console.ReadLine();
}
while (!decimal.TryParse(input, out goal));
Console.WriteLine("Please enter the numbers (separated by spaces)");
input = Console.ReadLine();
string[] elementsText = input.Split(' ');
List<decimal> elementsList = new List<decimal>();
foreach (string elementText in elementsText)
{
if (decimal.TryParse(elementText, out element))
{
elementsList.Add(element);
}
}
int i;
int j;
decimal tmp;
int[] arr1 = new int[10];
for (i = 0; i < elementsList.Count; i++)
{
for (j = i + 1; j < elementsList.Count; j++)
{
if (elementsList[i] < elementsList[j])
{
tmp = elementsList[i];
elementsList[i] = elementsList[j];
elementsList[j] = tmp;
}
}
}
Console.WriteLine("Please enter the maximum combination :");
NoDisplay = Convert.ToInt32(Console.ReadLine());
Solver solver = new Solver();
List<List<decimal>> results = solver.Solve(goal, elementsList.ToArray());
//results.Reverse();
Boolean recordexist = false;
foreach (List<decimal> result in results)
{
if (result.Count == NoDisplay)
{
recordexist = true;
foreach (decimal value in result)
{
Console.Write("{0}\t", value);
}
if (recordexist == true)
{
Console.WriteLine();
}
}
}
if (recordexist == false)
{
Console.WriteLine("No record exist");
}
Console.ReadLine();
}
}
我的問題是如何顯示的第一,第二和第三個答案只有 Like this
對不起我的英文不好,我希望有人能幫助我與此有關。我還是新的c#順便說一句。謝謝
你可以在這裏添加圖片本身,或通過應對粘貼的輸出本身。 Imgur鏈接可能會標記爲垃圾郵件。 – garg10may
好的。感謝您的建議:) – Nobody