我目前正在开展一项要求我覆盖服务的项目。
我有3个清单。 List1,List2和List3。我希望我的所有列表都能处理3到3倍的任何数字。
计数将进入。如果数字为1,请转到列表1.如果数字为4,请转到List1。如果数字是9,请转到清单3.
例如:
List1 will deal with 1, 4, 7, 10, 13, 16 etc
List2 will deal with 2, 5, 8, 11, 14, 17 etc
List3 will deal with 3, 6, 9, 12, 15, 18 etc
我希望这是有道理的。
我不喜欢设置表格或案例,而是选择简单的数学方法。
谢谢
答案 0 :(得分:2)
您需要使用模块化数学。要做到这一点,你只需要:
int listNumber = input % 3;
对于任何正整数,这将输出0,1或2。在这种情况下,0表示列表3。
你如何使用它取决于你的列表存储方式等等,但希望这应该是一个简单的练习。
答案 1 :(得分:1)
只需使用Modulus功能即可。它从除法运算中返回余数。
int number = 4;
int result = number % 3;
这里的结果将是1,这是必需的等等。
这是以3的C#
的倍数查找数字的最佳方法答案 2 :(得分:0)
像
这样的东西var listSelector = number % 3;
switch(listSelector)
{
case 0:
list3.add(number);
break;
case 1:
list1.add(number);
break;
case 2:
list2.add(number);
break;
}
0将作为0%3 == 0
进入list3答案 3 :(得分:0)
var lists = new[] { new List<int>(), new List<int>(), new List<int>() }; var listToDoStuffWith = lists[inputNumber % 3];