0

我有一个函数返回我的 int 值列表取决于值的一部分:

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],但应该
为 GetColumn(17, 1) - [6,7,8,9,10,11返回 [0,1,2,3,4,5] ]
for GetColumn(17, 2) - [12,13,14,15,16]

对于 16,它应该返回:对于 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]

我应该改变我的功能?谢谢!

4

3 回答 3

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);

应该管用。我刚刚对其进行了测试,它适用于与 3 不同的行大小。

这是我得到公式的方法,绘制表格是整理此类算法的好习惯:

r:Remainder, c:column, ps:pagesize(如上计算)

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

如果将表格扩展为行大小 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 类似,当您为给定的余数与列构建表时,可以看到所需的列长度。我暂时不会写这个,因为在这里绘制表格需要太多时间,我相信你已经明白了。

于 2012-03-09T07:32:17.000 回答
0

The integer division rounds to towards zero. So 17/3 = 5 and -17/3 = -5
I think what you want is to round to the next integer like this

int pageSize = (int)Math.Ceiling(total / 3d);
于 2012-03-09T07:09:43.373 回答
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;
}

答案假设我们总是分成 3 列,并且只有 3 列。如果数字是可变的,则可以概括确定列的逻辑。

于 2012-03-09T07:46:01.410 回答