C#如何仅显示组合的第一个、第二个和第三个答案?
我有使用组合方法的编码C#如何仅显示组合的第一个、第二个和第三个答案?,c#,sorting,console-application,C#,Sorting,Console Application,我有使用组合方法的编码 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 el
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();
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
班级计划
{
静态void Main(字符串[]参数)
{
字符串输入;
int节点显示;
十进制目标;
十进制元素;
做
{
Console.WriteLine(“请输入目标:”);
input=Console.ReadLine();
}
而(!decimal.TryParse(输入,输出目标));
Console.WriteLine(“请输入数字(用空格分隔)”);
input=Console.ReadLine();
string[]elementsText=input.Split(“”);
列表元素列表=新列表();
foreach(elementsText中的字符串elementText)
{
if(decimal.TryParse(elementText,out元素))
{
元素列表。添加(元素);
}
}
int i;
int j;
十进制tmp;
int[]arr1=新int[10];
对于(i=0;i
我的问题是如何只显示第一、第二和第三个答案
对不起,我的英语不好,我希望任何人都能帮我。顺便说一句,我还是新来的。谢谢
我的问题是如何显示第一、第二和第三个答案
只是
只要创建一个计数器
变量,每次显示结果
列表的子列表时,该变量的增量为1
,当它达到3
时,我们就可以中断
循环
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++;
}
}
int计数器=0;
foreach(在结果中列出结果)
{
如果(计数器==3)中断;
if(result.Count==NoDisplay)
{
recordexist=true;
foreach(结果中的十进制值)
{
写入(“{0}\t”,值);
}
if(recordexist==true)
{
Console.WriteLine();
}
计数器++;
}
}
我认为最简单的选择是使用Linq方法(过滤结果)和(获取前n个结果):
您可以在此处添加图像本身,也可以通过粘贴来添加输出本身。Imgur链接可能会被标记为垃圾邮件。好的。谢谢你的建议:)
var filteredResults = results
.Where(r => r.Count == NoDisplay)
.Take(3);
foreach (var result in filteredResults)
{
foreach (decimal value in result)
{
Console.Write("{0}\t", value);
}
Console.WriteLine();
}