如何计算整数的除法和模数?

时间:2011-03-21 20:10:51

标签: c#

如何计算C#中整数的除法和模数?

5 个答案:

答案 0 :(得分:114)

在提出此类问题之前,请查看MSDN documentation

  

分割两个整数时,结果始终为整数。例如,7/3的结果是2.要确定7/3的余数,请使用余数运算符(%)。

int a = 5;
int b = 3;

int div = a / b; //quotient is 1
int mod = a % b; //remainder is 2

答案 1 :(得分:70)

还有Math.DivRem

quotient = Math.DivRem(dividend, divisor, out remainder);

答案 2 :(得分:14)

有趣的事实!

'模数'操作定义为:

a % n ==> a - (a/n) * n

价:Modular Arithmetic

所以你可以滚动你自己的,虽然它比内置的%运算符慢得多:

public static int Mod(int a, int n)
{
    return a - (int)((double)a / n) * n;
}

编辑:哇,最初在这里做错了,感谢@joren抓住我

现在我在这里依赖的事实是C#中的div + cast-to-int相当于Math.Floor(即,它会降低分数),但是“真正的”实现将会像:

public static int Mod(int a, int n)
{
    return a - (int)Math.Floor((double)a / n) * n;
}

事实上,您可以通过以下方式查看%和“真模数”之间的差异:

var modTest =
    from a in Enumerable.Range(-3, 6)
    from b in Enumerable.Range(-3, 6)
    where b != 0
    let op = (a % b)
    let mod = Mod(a,b)
    let areSame = op == mod
    select new 
    { 
        A = a,
        B = b,
        Operator = op, 
        Mod = mod, 
        Same = areSame
    };
Console.WriteLine("A      B     A%B   Mod(A,B)   Equal?");
Console.WriteLine("-----------------------------------");
foreach (var result in modTest)
{
    Console.WriteLine(
        "{0,-3} | {1,-3} | {2,-5} | {3,-10} | {4,-6}", 
        result.A,
        result.B,
        result.Operator, 
        result.Mod, 
        result.Same);
}

结果:

A      B     A%B   Mod(A,B)   Equal?
-----------------------------------
-3  | -3  | 0     | 0          | True  
-3  | -2  | -1    | -1         | True  
-3  | -1  | 0     | 0          | True  
-3  | 1   | 0     | 0          | True  
-3  | 2   | -1    | 1          | False 
-2  | -3  | -2    | -2         | True  
-2  | -2  | 0     | 0          | True  
-2  | -1  | 0     | 0          | True  
-2  | 1   | 0     | 0          | True  
-2  | 2   | 0     | 0          | True  
-1  | -3  | -1    | -1         | True  
-1  | -2  | -1    | -1         | True  
-1  | -1  | 0     | 0          | True  
-1  | 1   | 0     | 0          | True  
-1  | 2   | -1    | 1          | False 
0   | -3  | 0     | 0          | True  
0   | -2  | 0     | 0          | True  
0   | -1  | 0     | 0          | True  
0   | 1   | 0     | 0          | True  
0   | 2   | 0     | 0          | True  
1   | -3  | 1     | -2         | False 
1   | -2  | 1     | -1         | False 
1   | -1  | 0     | 0          | True  
1   | 1   | 0     | 0          | True  
1   | 2   | 1     | 1          | True  
2   | -3  | 2     | -1         | False 
2   | -2  | 0     | 0          | True  
2   | -1  | 0     | 0          | True  
2   | 1   | 0     | 0          | True  
2   | 2   | 0     | 0          | True  

答案 3 :(得分:13)

使用/运算符执行分部:

result = a / b;

模块除法使用%运算符完成:

result = a % b;

答案 4 :(得分:-4)

从用户中读取两个整数。然后计算/显示余数和商,

// When the larger integer is divided by the smaller integer
Console.WriteLine("Enter integer 1 please :");
double a5 = double.Parse(Console.ReadLine());
Console.WriteLine("Enter integer 2 please :");
double b5 = double.Parse(Console.ReadLine());

double div = a5 / b5;
Console.WriteLine(div);

double mod = a5 % b5;
Console.WriteLine(mod);

Console.ReadLine();