2012-03-09 23 views
0

我有一個返回我列表int類型的功能取決於價值的一部分:除以3,在C#中的功能

private List<int> GetColumn(int total, int column) 
{ 
    List<int> retList = new List<int>(); 

    if (total <= 0) 
    { 
     return retList; 
    } 

    if (column < 0 || column > 2) 
    { 
     column = 0; 
    } 

    int pageSize = total/3; 

    int startIndex = column * pageSize; 
    int endIndex = column * pageSize + pageSize; 

    if (endIndex > total) 
    { 
     endIndex = total; 
    } 

    for (int i = startIndex; i < endIndex; i++) 
    { 
     retList.Add(i); 
    } 

    return retList; 

} 

,但它的工作原理錯誤的,因爲: GetColumn(17,0)

它返回[0,1,2,3,4],但應返回[0,1,2,3,4,5]
GetColumn(17,1) - [6,7,8, (17,2) - [12,13,14,15,16]

對於16個用於GetColumn(17,2)的
它應返回:GetColumn(16,0) - [0,1,2,3,4,5]
對於GetColumn(16,1) - [6,7,8,9,10]
返回: GetColumn(16,2) - [11,12,13,14,15]

我應該改變什麼功能?謝謝!

+1

您的第一個示例爲(17,1)調用返回6個元素,但爲(17,0)和(17,2)調用返回5個元素。如果元素的數量不能被3整除,這個邏輯將如何工作?應該有一個隨機的「頁面」獲得額外的元素,還是總是中間的?如果有兩個「額外」元素呢?中間和第一個,還是中間和最後一個? – 2012-03-09 06:45:14

+0

'int'除以'int'仍然是一個'int'。至少有一個操作數必須是'float' /'double'以使結果爲非'int'。 – 2012-03-09 06:45:31

+0

不,在這種情況下不需要劃分多於3個 – ihorko 2012-03-09 07:10:30

回答

3

如果這是你所需要的(數量的增加縱列但需要行第一被填充):

for 16: 
0 6 11 
1 7 12 
2 8 13 
3 9 14 
4 10 15 
5 

for 17: 
0 6 12 
1 7 13 
2 8 14 
3 9 15 
4 10 16 
5 11 

需要定義哪一列獲得餘數:

int remainder = total % 3; 

如果餘數爲1,只有第一列是6個元素。如果餘數爲2,則第一列爲6個元素。你需要根據這個來計算startIndex和endIndex。

所以;

int pageSize = total/3; 
int remainder = total % 3; 

int startIndex = column * pageSize + min(column, remainder); 
int endIndex = startIndex + pageSize + (remainder > column ? 1 : 0); 

應該工作。我只是測試它,它適用於不同的rowsizes比3

這裏是我得到的公式,繪製表格是整理出這樣algortihms好的做法:

R:餘,C:柱,PS:頁面大小(如上計算)

StartingIndex: 
. |r:0 |r:1 |r:2 
---------------------- 
c:0|0 |0  |0 
---------------------- 
c:1|ps |ps+1 |ps+1 
---------------------- 
c:2|ps*2|ps*2+1|ps*2+2 

可以看到的圖案,如果你對ROWSIZE 4延伸的表:

StartingIndex: 
. |r:0 |r:1 |r:2 |r:3 
------------------------------ 
c:0|0 |0  |0  |0 
------------------------------ 
c:1|ps |ps+1 |ps+1 |ps+1 
------------------------------ 
c:2|ps*2|ps*2+1|ps*2+2|ps*2+2 
------------------------------ 
c:3|ps*3|ps*3+1|ps*3+2|ps*3+3 

要添加的值是相關的最小列和其餘部分

對於endIndex,類似地,當您爲給定的餘數vs列創建表時,可以看到期望的列長度。現在我不會寫這些,因爲在這裏畫表需要很多時間,我相信你已經有了這個想法。

+0

是的,正是我需要的,但是如何在函數爲列1和2返回正確的範圍? – ihorko 2012-03-09 07:38:19

+0

我更新了我的答案,並添加了更多解釋。 – bmkorkut 2012-03-09 08:06:53

+0

謝謝,但是startIndex和endIndex對於不同的列是錯誤的:( – ihorko 2012-03-09 09:42:43

0

整數除法舍入爲零。所以17/3 = 5和-17/3 = -5
我想你想要的是四捨五入到下一個整數這樣

int pageSize = (int)Math.Ceiling(total/3d); 
+0

,因爲對於GetColumn(16,0)它返回5個值,但應返回6,GetColumn(16,1) - 5,GetColumn (16,2) - 5 – ihorko 2012-03-09 07:17:03

+0

是的,我很抱歉的錯誤。我只是修復它。 – Elias 2012-03-09 07:23:08

+0

它也不能正確工作,因爲對於第一列和第二列它返回6個值,但是對於第三個只有4個,但是應該返回:6,5,5 – ihorko 2012-03-09 07:30:41

0

如果我理解正確的要求是:做

If the number is 3n, divide it in 3 groups of n, n and n elements. 
If the number is 3n+1, divide it in 3 groups of n+1, n and n elements. 
If the number is 3n+2, divide it in 3 groups of n+1, n+1 and n elements. 

最好的辦法是讓你的代碼是明確的,並避免任何「聰明」的邏輯。 直進分裂歸結爲:

If the number is 3n, the divisions are: 
    0 .. n-1 
    n .. 2n-1 
    2n .. 3n-1 
If the number is 3n+1, the divisions are: 
    0 .. n 
    n+1 .. 2n 
    2n+1 .. 3n 
If the number is 3n+2, the divisions are: 
    0 .. n 
    n+1 .. 2n+1 
    2n+2 .. 3n+1 

在C#中,這將是這樣的:

public static List<int> Divide3Columns(int total, int column) 
{ 
    int startIndex = 0; 
    int endIndex = 0; 
    int pageSize = total/3; 

    if (total % 3 == 0) 
    { 
    startIndex = column * pageSize; 
    endIndex = (column + 1) * pageSize - 1; 
    } 

    if (total % 3 == 1) 
    { 
    if (column == 0) 
    { 
     startIndex = 0; 
     endIndex = pageSize; //pageSize + 1 elements; 
    } 
    else 
    { 
     startIndex = column * pageSize + 1; 
     endIndex = (column + 1) * pageSize; 
    } 
    } 

    if (total % 3 == 2) 
    { 
    if (column == 2) 
    { 
     startIndex = 2 * pageSize + 2; 
     endIndex = 3 * pageSize + 1; //same as total - 1; 
    } 
    else 
    { 
     startIndex = column * (pageSize + 1); 
     endIndex = (column + 1) * pageSize + column; 
    } 
    } 

    List<int> result = new List<int>(); 
    for (int i = startIndex; i <= endIndex; i++) 
    { 
    result.Add(i); 
    } 
    return result; 
} 

答案假定我們永遠divide in 3, and only 3 columns。如果數字是可變的,確定列的邏輯可以推廣。

+0

它不起作用 – ihorko 2012-03-09 10:05:54

+0

@ihorko修復了它並添加了一個更完整的示例 – SWeko 2012-03-09 12:50:57