将 X 除以 Y,return 最后部分集合中的项目数

Divide X by Y, return number of items in last, partial set

我可能遗漏了一些非常简单的东西,但我想弄清楚如何计算我将 X 除以 Y 后剩下的东西。我不是说余数,我的意思是,例如如果我将 100 除以 7 => 6 组 15 + 一组 10,我如何得到 10?

我没有要显示的代码,因为我不知道从哪里开始。 X和Y都是整数。

I don't mean the remainder

是的,你的意思是余数。

如果用 100 除以 15,商为 6,余数为 10。

一样使用模运算符
int remainder = 100 % 15; // This will return 6

int quotient = 100/15;  // This will return 10

它是一个模运算符,而不是:

var result = x / y;

试试这个:

var result = x % y;

编辑。 好的,你很不清楚,但我认为以下解决方案之一就是你想要实现的。

S1。这样做:

int x = 100/15;
int z = 15 * x;
int y = 100 - z; // and You got Your 10

S2。这样做:

int x = 100/7;
if ( x * 7 != 100)
{
    int GroupSize = x+1;
    int rest = 100 - GroupSize;
}

这不是使用模数那么简单。繁琐的一点是根据组数计算您的初始组大小。

试试这个:

int population = 100;
int numberOfGroups = 7;
int groupSize = (population + numberOfGroups - 1)/numberOfGroups;

Console.WriteLine(groupSize);

int remainder = population%groupSize;

Console.WriteLine(remainder);