0

我正在使用 Visual Studio 在 C# 中编写一个小型控制台应用程序。

我想写两个小程序,都将通过控制台输入六个整数,然后 if 语句将比较所有六个值,最终结果将是控制台输出打印输入的六个值以及从最高到最低的顺序值. 我绞尽脑汁想弄清楚如何做到这一点。

完成此操作后,我将编写第二个程序来使用我认为会更整洁的列表来执行此操作。

所以我认为应该有六个整数来保存输入的值,然后是六个整数来保存有序值,中间的 if 语句可以计算出从最高到最低的顺序。

我试图做嵌套的 if,但它变得很乱,我决定放弃它并重新开始。

我将不胜感激。

这是我正在编写的代码:

  int one;
        int two;
        int three;
        int four;
        int five;
        int six;


        int firsthighest;
        int secondhighest;
        int thirdhighest;
        int fourthhighest;
        int fifthhighest;
        int sixthhighest;



        Console.WriteLine("Enter a value");
        one = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter a value");
        two = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter a value");
        three = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter a value");
        four = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter a value");
        five = int.Parse(Console.ReadLine());

        Console.WriteLine("Enter a value");
        six = int.Parse(Console.ReadLine());



        if (one > two)
        {
            if (one > three)
            {
                if (one > four)
                {

                    if (one > five)
                    {

                        if (one > six)
                        {
                            firsthighest = one;

                        }

                    }

                }
            }

        }

        else if (two > three)
        {
            if (two > four)
            {

                if (two > five)
                {

                    if (two > six)
                    {
                        firsthighest = two;

                    }

                }

            }

        }

        else if (three > four)
        {


            if (three > five)
            {

                if (three > six)
                {
                    firsthighest = three;

                }

            }

        }
4

5 回答 5

4

这是作业吗?

我建议跳过 if 并直接进入你的第二个程序 - 将所有 6 个放在一个数组中并对其进行排序。

于 2012-10-09T16:34:34.527 回答
2

您可以使用数组,然后对其使用排序算法,例如冒泡排序。

于 2012-10-09T16:35:09.723 回答
2

您可以使用任何排序算法。将其存储在六个变量或一个数组中都没有关系。如果您打算使用Enumerable.OrderBy,那么它在内部只是实现了这种排序算法之一。

于 2012-10-09T16:37:16.197 回答
2

我不确定您甚至计划如何使用一组嵌套if语句来实现排序算法。让我们考虑一下。假设我按以下顺序输入了六个整数:

8,3,6,1,9,10

现在,8 肯定大于 3,所以如果我这样写第一组if's:

if (one > two) { ... }

这真的告诉我们什么?只有one大于two,但它肯定没有告诉我们列表中的位置one。所以,假设我嵌套了所有可能的场景:

if (one > two) {
    if (one > three) {
        if (one > four) {
            if (one > five) {
                if (one > six) { ... }
            }
        }
    }
}

会发生什么?这会告诉我们什么?好吧,它会告诉我们five大于one……但问题是six大于five

这个简短的评估应该告诉你这不是一个好的方法,所以如果你想证明这是一个不好的方法,那就去吧。

现在,让我们继续解决问题,并将这些整数放入一个列表中:

var list = new List<int> { one, two, three, four, five, six };
var result = list.OrderByDescending(x => x);

在内部,这是一种真正的排序算法,它通过多次将每个值与所有其他值进行比较来遍历每个值。但是,它也使用实时索引来帮助减少迭代进行了更优化。

于 2012-10-09T17:09:09.370 回答
1

要检查的一个关键字是排序网络

对于 6 个输入,至少需要 12 个具有恒定结构的比较和交换操作。1-8 个元素的最佳结构是已知的。4 元素最优排序网络的一个例子是:

 void f(int a, int b) { if (arr[a]>arr[b]) swap(arr,a,b); }
 f(1,2); f(3,4); f(1,3); f(2,4); f(2,3);

这些结构不能很好地嵌套......

于 2012-10-09T18:48:20.413 回答