将X除以Y,返回最后一个项目,部分设置

时间:2015-03-19 14:29:45

标签: c# math

我可能错过了一些非常简单的事情,但是我试图弄清楚如何计算在我将X除以Y之后遗留下来的东西。我不是指余数,我的意思是,例如如果我将100除以7 => 6组15 +一组10,我怎么得到10?

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

3 个答案:

答案 0 :(得分:5)

它不像使用模数那么简单。这个繁琐的位是从组数计算你的初始组大小。

试试这个:

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

Console.WriteLine(groupSize);

int remainder = population%groupSize;

Console.WriteLine(remainder);

答案 1 :(得分:0)

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

答案 2 :(得分:0)

它是模运算符,而不是:

var result = x / y;

试试这个:

var result = x % y;

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

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