简体   繁体   English

C#如何仅显示组合的第一个,第二个和第三个答案?

[英]C# How to display the first, second and third answer only of the combination?

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. 我还是C#btw的新手。 Thank you 谢谢

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. 只需创建一个counter变量,每次显示results列表的子列表时该counter变量就增加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++;
       }
}

I would say the simplest option is to use the Linq methods Where (to filter the results) and Take (to take the first n results): 我想说的最简单的选择是使用Linq方法Where (过滤结果)和Take (获取前n个结果):

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在C#中将两个文本框值相乘并显示第三个答案? - How to multiply two textbox values and display answer to third in c#? 如何在C#中的第一个和第二个方法之后执行第三个方法 - how to execute a third method after first and second method in c# 在 C# 中,当我单击第二个表单的按钮时,如何在第一个表单中打开第三个表单 - In C# how to open third form inside first form when i click a button of the second form 当第二层在第一层和第三层之间时,如何避免它们之间的碰撞? 统一二维 C# - how to avoid collision between first and third layer when second is between them? Unity 2D C# 如何在C#中显示小数点答案 - How to display a decimal point answer in c# C#迭代没有任何答案,前2次。 第三遍输出一次又一次 - c# iteration with no answer first 2 times. Output on third try over and over again 仅当第一个对象实现第二个对象所需的接口时,如何在 C# 中在运行时组合两个对象? - How to Compose two objects at runtime in C# only if the first object implements interfaces required by the second? c# - 如何将值从第一种形式传递到第三种形式? - How to pass value from first form to the third form on c#? 如何使用C#仅将第一页从Multipage Blob .Tiff图像转换和显示为.Jpeg - How to Convert and display only the first page from Multipage Blob .Tiff image to .Jpeg using C# 如何制作第三个文本框取决于c#.net中的第一个和第二个文本框 - How to make the third textbox is depends on first and second textbox in c#.NET
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM