C# 除以3,函数在C中#
我有一个函数,它根据部分值返回int值列表:C# 除以3,函数在C中#,c#,asp.net,divide,C#,Asp.net,Divide,我有一个函数,它根据部分值返回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) {
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;
}
private List GetColumn(整数总计,整数列)
{
List retList=新列表();
若有(共2个)
{
列=0;
}
int pageSize=总计/3;
int startIndex=列*页面大小;
int endIndex=列*pageSize+pageSize;
如果(endIndex>total)
{
endIndex=总数;
}
对于(int i=startIndex;i
但这是错误的,因为:
GetColumn(17,0)
它返回[0,1,2,3,4],但应返回[0,1,2,3,4,5]对于GetColumn(17,1)-[6,7,8,9,10,11]
对于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]
我应该改变我的功能吗?谢谢 整数除法向零舍入。所以17/3=5和-17/3=-5
我想你想要的是像这样四舍五入到下一个整数
int pageSize = (int)Math.Ceiling(total / 3d);
整数除法向零舍入。所以17/3=5和-17/3=-5
我想你想要的是像这样四舍五入到下一个整数
int pageSize = (int)Math.Ceiling(total / 3d);
如果这是您需要的(数字按列增加,但需要先填充行):
您需要定义哪个列将获得余数:
int remainder = total % 3;
如果余数是1,则只有第一列是6个元素。如果余数为2,则第一列和第二列为6个元素。您需要根据此公式计算startIndex和endIndex
所以,
应该有用。我刚刚测试了它,它适用于不同的行大小,而不是3
下面是我如何得到公式的,绘制表格是整理此类算法的良好实践:
r:余数,c:列,ps:页面大小(如上计算)
如果扩展表中的行大小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类似,在为给定的余数和列构建表时,可以看到所需的列长度。我现在不写了,因为在这里画表格花了太多时间,我相信你已经有了这个想法。如果这是你需要的(数字按列增加,但行需要先填充):
您需要定义哪个列将获得余数:
int remainder = total % 3;
如果余数是1,则只有第一列是6个元素。如果余数为2,则第一列和第二列为6个元素。您需要根据此公式计算startIndex和endIndex
所以,
应该有用。我刚刚测试了它,它适用于不同的行大小,而不是3
下面是我如何得到公式的,绘制表格是整理此类算法的良好实践:
r:余数,c:列,ps:页面大小(如上计算)
如果扩展表中的行大小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类似,在为给定的余数和列构建表时,可以看到所需的列长度。我现在不写了,因为在这里画表格花了太多时间,我相信你已经有了这个想法。如果我理解正确,要求是:
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;
}
publicstaticlist Divide3Columns(int-total,int-column)
{
int startIndex=0;
int-endIndex=0;
int pageSize=总计/3;
如果(总计%3==0)
{
startIndex=列*页面大小;
endIndex=(列+1)*pageSize-1;
}
如果(总计%3==1)
{
如果(列==0)
{
startIndex=0;
endIndex=pageSize;//pageSize+1个元素;
}
其他的
{
startIndex=列*pageSize+1;
endIndex=(列+1)*页面大小;
}
}
如果(总计%3==2)
{
如果(列==2)
{
startIndex=2*pageSize+2;
endIndex=3*pageSize+1;//与total-1相同;
}
其他的
{
startIndex=列*(页面大小+1);
endIndex=(列+1)*pageSize+列;
}
}
列表结果=新列表();
对于(int i=startIndex;i,如果我正确理解,则要求为:
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;
}
publicstaticlist Divide3Columns(int-total,int-column)
{
int startIndex=0;
int-endIndex=0;
int pageSize=总计/3;
如果(总计%3==0)
{
startIndex=列*页面大小;
endIndex=(列+1)*pageSize-1;
}
如果(总计%3==1)
{
如果(列==0)
{
startIndex=0;
endIndex=pageSize;//pageSize+1个元素;
}
其他的
{
startIndex=列*pageSize+1;
endIndex=(列+1)*页面大小;
}
}
如果(总计%3==2)
{
如果(列==2)
{
startIndex=2*pageSize+2;
endIndex=3*pageSize+1;//与total-1相同;
}
其他的
{
startIndex=列*(页面大小+1);
endIndex=(列+1)*pageSize+列;
}
}
列表结果=新列表();
对于(int i=StistCe指标;i您的第一个示例),返回(17,1)调用的6个元素,但是(17,0)和(17,2)调用的5个元素。如果元素的数目不能被三整除,那么这个逻辑将如何工作?如果一个随机的“页面”得到额外的元素,或者总是中间的一个元素?如果有两个“额外的”元素,那该怎么办?元素?中间元素和第一个元素,还是中间元素和最后一个元素?<代码> int /代码>除以<代码> int >代码>仍然是<代码> int <代码>。至少有一个操作数必须是<代码>浮点< /代码> /<代码>双< /代码>,以使结果为非-<代码> int /代码>。不,不需要划分超过3f的f。