2015-11-12 45 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

更新了我的主題,不允許使用排序 –