2015-11-12 44 views
-5

我有一个assingment,我有点失落。在用户输入的10个(或更少)数字的数组中(我完成了这部分),我需要找到第二小的数字。我的朋友给我这个代码,但我很难理解它,并写在C#中:在c中排序数组数字#

解决了它! :

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      int vnesena; 
      int? min1 = null; 
      int? min2 = null; 
      for(int i=1; i<11; i=i+1) 
      { 
       Console.WriteLine("Vpiši " + i +"." + " število: "); 
       vnesena = Convert.ToInt32(Console.ReadLine()); 

       if (vnesena == 0) 
       { 
        break; 

       } 
       if (min1 == null || vnesena < min1) 
       { 
        min2 = min1; 
        min1 = vnesena; 
       } 
       else if (vnesena != min1 && (min2==null || vnesena<min2)) 
       { 
        min2 = vnesena; 
       } 



      } 


      if (min1 == null || min2 == null) 
      { 
       Console.WriteLine("Opozorilo o napaki"); 
      } 
      else 
      { 
       Console.WriteLine("Izhod: " + min2); 
      } 


      Console.ReadKey(); 


     } 
    } 
} 
+1

这不是C# - 它是伪代码。 – Tim

+6

如果你问你的朋友C#的帮助,他发给你,他不是你的朋友! – Jamiec

+2

向我们展示您迄今为止撰写的C#。 –

回答

3

这段代码太复杂了,所以试试这样。

int[] numbers = new int[10]; 
for (int i = 0; i < 10; i++) 
{ 
    numbers[i] = int.Parse(Console.ReadLine()); 
} 
Array.Sort(numbers); 
Console.WriteLine("Second smallest number: " + numbers[1]); 

如果代码是不是太明显,让我解释一下:

  1. 申报的10个整数
  2. 数组环路10十次,每一次,要求用户输入&地方输入作为数组的整数
  3. 对数组进行排序,以便每个数字的编号顺序(最小的第一个,最后一个)。
  4. 第一个整数是最小的(在索引0处输入,所以数字[0]),次小数显然是数字[1]。

当然,对于这段代码的工作,你必须在控制台程序中使用这段代码。

正如你没有提到,如果你被允许使用内置排序函数等,我假定Array.Sort()是有效的。

编辑:您更新了您的主题,因此我会更改我的代码以匹配标准。

int[] numbers = new int[10]; 
bool tooShortInput = false; 
for (int i = 0; i < 10; i++) 
{ 
    int input = int.Parse(Console.ReadLine()); 
    if (input != 0) 
    { 
     numbers[i] = input; 
    } 
    else 
    { 
     if (i == 2) 
     { 
      Console.WriteLine("You only entered two numbers!"); 
      tooShortInput = true; 
      break; 
     } 
     else 
     { 
      for (int j = 0; j < 10; j++) 
      { 
       if (numbers[j] == 0) 
       { 
        numbers[j] = 2147483647; 
       } 
      } 
      break; 
     } 
    } 
} 
// Sort the array  
int temp = 0; 

for (int write = 0; write < numbers.Length; write++) { 
    for (int sort = 0; sort < numbers.Length - 1; sort++) { 
     if (numbers[sort] > numbers[sort + 1]) { 
     temp = numbers[sort + 1]; 
     numbers[sort + 1] = numbers[sort]; 
     numbers[sort] = temp; 
     } 
    } 
} 

if (!tooShortInput) 
{ 
    Console.WriteLine("Second smallest number: " + numbers[1]); 
} 

如果您不明白更新的代码,请告诉我,我会解释。

注意:这是用android手机进行快速编码和测试,所以显然这个代码不是5星级的质量,甚至不接近,但它符合:-)。

问候,TuukkaX。

+0

更新了我的主题,不允许使用排序 –

+1

@ReneVucko更新了我的代码。不使用内置的排序功能,并且完美地工作。该代码很不好说,至少说,但它的作品。代码中的排序机制称为“冒泡排序”。 – TuukkaX

2

套用给定的代码:

  1. 设置2个变量不了了之。 (这是为了让有可以检查后做。如果你想使用null这里一个想法int?都可以使用。通过数值
  2. 开始循环。
  3. 获取下一个值。
  4. 如果最低ISN” t设置或新值低于最小值,则用前一个最低值替换第二个最低值,并用输入的新值替换
  5. 否则,检查新值是否与最小值不相同,并且if最小值未设置或输入值低于第二低值,则用此新值替换第二低值。
  6. 一旦厕所p完成,如果最小值没有被填充,那么输出没有这样的值,否则输出第二低的值。

想象一下,如果您必须手动执行此操作。当你通过数组时,你可能会跟踪最低值和最低值,程序仅仅是自动执行这个过程。问题是什么?


这是一个粗略的翻译,你的朋友给你的东西并不难以翻译到我的脑海。

 int enteredValue; 
     int? smallest = null, secondSmallest = null; 

     for (int i = 0; i < 10; i = i + 1) 
     { 
      Console.WriteLine("Vpiši " + i+1 + " število: "); 
      enteredValue = Convert.ToInt32(Console.ReadLine()); 
      if (smallest==null || enteredValue<smallest) { 
        secondSmallest=smallest; 
        smallest = enteredValue; 
      } else if (enteredValue!=smallest && enteredValue<secondSmallest) { 
        secondSmallest= enteredValue; 
      } 
     } 
+0

注意:正确的算法的解释可以通过https://www.bing.com/search?q=c%23+select+kth+min找到像[如何找到未分类长度数组中的第k个最大元素n(O(n)?](http://stackoverflow.com/questions/251781/how-to-find-the-kth-largest-element-in-an-unsorted-array-of-length-n-in -on) –

+0

仍不知如何解决此问题 –

+1

您在这里期待的答案有多接近?有没有你想要的部分?我甚至翻译过朋友的算法。如果你想要更多,我会建议聘请私人导师为你做这项工作,因为这已经非常接近IMO了。 –

0

为什么使用循环而没有利用Array.Sort方法?

 int[] numbers = new int[4] { 4, 2, 6, 8 }; 

     Array.Sort(numbers); 

     int secondSmallestNumber = numbers[1]; 
+0

更新了我的主题,不允许使用排序 –