2012-12-26 56 views
5

大家好,今天祝大家聖誕節快樂。
我有問題,也許有人可以幫助我。
我有一個列表框,用戶可以輸入十進制數字。
比方說,他們將進入5號:在C中計算(複雜)十進制數字數組#

1.1 
1.2 
1.3 
1.4 
1.5 

我需要得到那些5個號碼的所有變化的總和。 例如1.1 and 1.2然後1.1 1.2 1.3然後1.1 1.2 1.3 1.4,然後1.2 1.4 1.5然後1.1 1.3 1.5的總和。
我開始的東西,但認爲經過所有變化一次只跳過一個數字:

List<Double[]> listNumber = new List<double[]>();    
Double[] array;    
for (int i = 0; i < listBox1.Items.Count; i++) 
{ 
    array = new Double[listBox1.Items.Count];     
    for (int k = 0; k < listBox1.Items.Count; k++) 
    { 
     if (!k.Equals(i)) 
     { 
      array[k] = (Convert.ToDouble(listBox1.Items[k]));      
     } 
    } 
    listNumber.Add(array); 
} 

我需要找到一種方法如何計算的方式我想,如果有人可以給我森達知道它會是偉大的聖誕禮物:) 在此先感謝,Laziale

+0

這是一個家庭作業..? – MethodMan

回答

1

在您的第一次嘗試,您的代碼只計算所有可能的對的總和。從你的描述中,你也想找到三個數字的總和等。

如果總是有5個十進制數,那麼你可以簡單地有5個for循環。然而一個更通用的設計是清潔

double[] input = double[5]; //Pretend the user has entered these 
int[] counters = int[input.Length]; //One for each "dimension" 
List<double> sums = new List<double>(); 

for (int i = 0; i < counters.Length; i++) 
    counters[i] = -1; //The -1 value allows the process to begin with sum of single digits, then pairs, etc.. 

while (true) 
{ 
    double thisSum = 0; 
    //Apply counters 
    for (int i = 0; i < counters.Length; i++) 
    { 
     if (counters[i] == -1) continue; 

     thisSum += input[counters[i]]; 
    } 

    //Increment counters 
    counters[0]++; //Increment at base 
    for (int i = 0; i < counters.Length; i++) 
    { 
     if (counters[i] >= counters.Length) 
     { 
      if (i == counters.Length - 1) //Check if this is the last dimension 
       return sums; //Exhausted all possible combinations 

      counters[i] = 0; 
      counters[i+1]++; 
     } 
     else 
      break; 
    } 
} 

這是不以避免兩次加相同數量的任何代碼(我會讓你努力完成自己的目標。提示:您可以簡單地在增量計數器部分之後執行此操作,同時在while循環中包含「增量計數器」部分和新的「檢查計數器」部分,當計數器是唯一的時候,在while循環之外打破...

注意:我沒有測試過這個代碼,但它會很接近,並且可能會有一兩個錯誤 - 讓我知道你是否需要任何幫助。

0

雖然我不是很熟悉C#,但我確信有一個更簡單的方法來做你想做的事情;除非當然,我錯過了一些東西。

爲什麼不爲List或Array中的每個元素做一個for循環,然後告訴它跳過它自己。 例子:

Double[] array = new Double[3]; 
array[0] = 1,1; 
array[1] = 1,2; 
array[2] = 1,3; 

Double sum = 0; 

for (int i = 0; i < array.Length ; i++) 
{ 
    for (int x = 0 ; x < array.Length ; x++) { 
     if (array[i] != array[x]) 
     { 
      sum = array[x] + array[x+1] // or [x-1] depending on the value of x, you should be able to work this out. 
     } 
    } 
} 

你應該能夠理解我通過檢查這個例子的意思。 當然,這是一個非常基本的原型,你要做的是擴大這個檢查方向,根據x的值,並有多個「總和」變量來存儲你的總和 - 取決於你的結果是什麼樣的結果,重新尋找。

- 我希望這有助於聖誕快樂。

+0

這並沒有給出所有的可能性。 – kmkaplan

1

正如我我的手機上輪廓:

開始與你的輸入列表,幷包含一個零輸出列表。

對於輸入中的每個數字,通過將當前輸入編號添加到當前輸出列表中的每個數字,創建一個新的雙精度列表;然後將此列表連接到輸出列表的末尾。

任選地,除去所述零並且每個輸入數的第一個實例,和任何重複:

例如對於您的示例輸入高達1.4:

0 
0 1.1 
0 1.1 1.2 2.3 
0 1.1 1.2 2.3 1.3 2.4 2.5 3.6 
0 1.1 1.2 2.3 1.3 2.4 2.5 3.6 1.4 2.5 2.6 3.7 2.7 3.8 3.9 5.0 
     1.2 2.3  2.4 2.5 3.6   2.6 3.7 2.7 3.8 3.9 5.0      
0

把你listBox,並在每個號碼的前面,要麼把一個0,以表明它會參與到你的總和或1,以表明它參與到你的總和。隨着1.1你的榜樣名單,1.21.31.41.51.1總和,1.2然後1.1 1.2 1.3然後1.1 1.2 1.3 1.4然後1.2 1.4 1.5然後1.1 1.3 1.5這將給你(我只寫1 S表示清晰,空的空間意味着0):

  |  |  | 1.1 |  | 
     |  | 1.1 | 1.2 | 1.2 | 1.1 
     |  | 1.2 | 1.3 | 1.4 | 1.3 
    1.1 | 1.2 | 1.3 | 1.4 | 1.5 | 1.5 
---+-----+-----+-----+-----+-----+----- 
1.1| 1   1  1   1 
1.2|  1  1  1  1 
1.3|    1  1   1 
1.4|     1  1 
1.5|       1  1 

正如現在可以用這樣的表示參見,列出這些數目的所有組合是現在類似於從0計數中到31(11111二進制,2⁵ - 1)。如果您對空序列不感興趣,則從1開始計數。

以下是將此計數轉換爲您想要的listNumber的示例代碼。請原諒我的語法,因爲我不知道C#。這也意味着這是未經測試的代碼。

Double[] array = new Double[listBox1.Items.Count]; 
for (int i = 0; i < listBox1.Items.count; i++) 
    array[k] = Convert.ToDouble(listBox1.Items[i]); 
int count = 2^array.Items.Count; 
List<Double>[] listNumber = new List<Double>[count]; 
for (int i = 0; i < listNumber.Items.Count; i++) { 
    listNumber[i] = new List<Double>(); 
    for (j = 0; j < array.Items.Count) 
     if (i & (1 << j) != 0) 
      listNumber[i].Add(array[j]); 
}