C#如何仅显示组合的第一个,第二个和第三个答案?(C# How to display the first, second and third answer only of the combinatio

编程入门 行业动态 更新时间:2024-10-25 16:28:27
C#如何仅显示组合的第一个,第二个和第三个答案?(C# How to display the first, second and third answer only of the combination?)

我有使用组合方法的编码。

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(); } }

这是输出

我的问题是我如何才能显示第一,第二和第三个答案就像这样

对不起,我的英语不好,我希望有人能帮助我。 我还是新的c#顺便说一句。 谢谢

I have coding that uses the combination method.

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(); } }

Here is the output

My question is how can i display the first,second and third answer only Like this

Sorry for my bad english, I hope anybody can help me with this. I still new on c# btw. Thank you

最满意答案

我的问题是我如何才能显示第一,第二和第三个答案?

简单地创建一个counter变量,每次显示results列表的子列表时,它会以1递增,当它达到3时,我们可以break循环。

int counter = 0; foreach (List<decimal> result in results) { if(counter == 3) break; if (result.Count == NoDisplay) { recordexist = true; foreach (decimal value in result) { Console.Write("{0}\t", value); } if (recordexist == true) { Console.WriteLine(); } counter++; } }

My question is how can i display the first,second and third answer only?

Simply create a counter variable which increments by 1 each time a sub-list of the results list has been displayed, when it reaches 3 then we can break out the loop.

int counter = 0; foreach (List<decimal> result in results) { if(counter == 3) break; if (result.Count == NoDisplay) { recordexist = true; foreach (decimal value in result) { Console.Write("{0}\t", value); } if (recordexist == true) { Console.WriteLine(); } counter++; } }

更多推荐

本文发布于:2023-08-04 05:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1409344.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:组合   第一个   第二个   第三个   答案

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!