1

我需要在存储在数组中的所有整数中找到大于 0 的最小值。我在stackoverflow上尝试了一些方法,但在所有情况下我的最小值仍然等于0。我应该在我的代码中进行哪些更改才能使其正常工作?

int[] userInput = new int[1000];
        int counter;

        Console.WriteLine ("Please input some numbers");

        for (counter = 0; counter < userInput.Length; counter++) {
            string line = Console.ReadLine ();

            if (line == "" || line == "stop") {
                break;
            } else {
                int.TryParse (line, out userInput [counter]);
            }
        }
        int min = 0;
        for(int i = 0; i < userInput.Length; i++)
        {
            if(userInput[i] > 0)
            {
                userInput[i] = min;
                break;
            }
        }
        for(int i = 0; i < userInput.Length; i++)
        {
            if(userInput[i] < min && userInput[i] > 0)
            {
                min = userInput [i];
            }
        }
        Console.WriteLine(min);
    }
}

}

我想在不使用 LINQ 的情况下做到这一点。

4

9 回答 9

2

一个例子。您可以使用 Praveen 的答案,但这只是一种选择。

int[] numbers = { -1, 0, 1, 2, 3, 4, 5 };

public int getMinimum(int[] array)
{
    // Since you need larger than 0
    int minimum = 1;

    foreach (int elem in array)
    {
        minimum = Math.Min(minimum, elem);
    }

    return minimum;
}

所以调用getMinimum(numbers)会返回1

希望能帮助到你。

于 2013-09-02T12:28:55.267 回答
1
int[] userInput = new int[1000];
int counter;

Console.WriteLine("Please input some numbers");

for (counter = 0; counter < userInput.Length; counter++)
{
    string line = Console.ReadLine();

    if (line == "" || line == "stop")
    {
        break;
    }
    else
    {
        int.TryParse(line, out userInput[counter]);
    }
}
int min = 0;
for (int i = 0; i < userInput.Length; i++)
{
    if (userInput[i] < min && userInput[i] > 0)
    {
        min = userInput[i];
    }
}
Console.WriteLine(min);

这将在数​​组中找到最小的数字。

于 2013-09-02T12:35:17.220 回答
0

在您当前的解决方案中,您将高于 0 的第一个实例更改为 0:

if(userInput[i] > 0)
{
    userInput[i] = min;
    break;
}

您需要将其更改为

min = userInput[i];

虽然这个 for 循环无论如何都是完全没有必要的(并且增加了复杂性)。您最好使用 int.MaxValue 将 min 设置为最大可能的 int 值,以便在下一个 for 循环中

if(userInput[i] < min && userInput[i] > 0)
{
    min = userInput [i];
}

第一个值将小于(或至少等于)min;

于 2013-09-02T12:33:41.903 回答
0

您必须将列表限制为高于零的所有条目,然后使用 LINQ 方法 Min() 查找最接近零的最低最小值。请参阅非 LINQ 版本作为下面的第二个示例。

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please input some numbers. Use SPACE as separator");
            string line = Console.ReadLine();
            var userInput = new List<int>();

            foreach (string entry in line.Split(' '))
            {
                int number;
                if (!string.IsNullOrEmpty(entry) && int.TryParse(entry, out number))
                    userInput.Add(number);
            }

            const int lowest = 0;

            Console.WriteLine("The lowest number above {0} is: {1}", lowest, userInput.Where(n => n > lowest).Min());

            Console.WriteLine("Press any key to finish.");
            Console.ReadKey();
        }
    }
}

非 LINQ 示例

using System;
using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Please input some numbers. Use SPACE as separator");
            string line = Console.ReadLine();
            var userInput = new List<int>();

            int min = int.MaxValue;

            if (!string.IsNullOrEmpty(line))
            {
                foreach (string entry in line.Split(' '))
                {
                    int number;
                    if (!string.IsNullOrEmpty(entry) && int.TryParse(entry, out number))
                        userInput.Add(number);
                }
            }

            const int lowest = 0;
            foreach(int number in userInput)
            {
                if (number > lowest)
                    min = Math.Min(number, min);
            }

            Console.WriteLine("The lowest number above {0} is: {1}", lowest, min);

            Console.WriteLine("Press any key to finish.");
            Console.ReadKey();
        }
    }
}
于 2013-09-02T12:33:54.840 回答
0

使用 LINQ 在一行中执行此操作:

int[] userInput = new int[1000];

var result = userInput.OrderBy(i=>i).SkipWhile(i=>i<=0).First()
于 2013-09-02T12:40:07.707 回答
0

尝试这个:

   int min;
    int found_positive = 0; // 1 if there's positive element
    for(int i = 0; i < userInput.Length; i++)
    {
      if(userInput[i]>0){
        if(found_positive==1){
          if(userInput[i]<min) min=userInput[i];
        }else{
          found_positive=1;
          min = userInput[i];
        }
      }
    }
于 2013-09-02T12:25:24.643 回答
0
    int[] userInput = new int[1000];
    int counter;

    Console.WriteLine ("Please input some numbers");

    for (counter = 0; counter < userInput.Length; counter++) {
        string line = Console.ReadLine ();

        if (line == "" || line == "stop") {
            break;
        } else {
            int.TryParse (line, out userInput [counter]);
        }
    }
    var min = userinput.Length>0?userInput[0]:0;
    for(int i = 1; i < userInput.Length; i++)
    {
        if(userInput[i] < min && userInput[i] > 0)
        {
            min = userInput [i];
        }
    }

这么多应该够了……

于 2013-09-02T12:27:51.810 回答
0

在一组值中获取最大值/最小值是一个非常流行(并且最终是微不足道的)编程问题。

您的帖子是同一问题的专门实例。在您的情况下,您希望获得一组整数中的最小正值。

您的代码几乎是正确的。仅替换第二个for块中的以下行:

 min = userInput[i];
于 2013-09-02T12:29:05.070 回答
-1
array1.Min()

看到这个

使用 Min 方法,我们找到变换后的最小元素或最小值。

于 2013-09-02T12:22:30.450 回答