我正在加密用户的输入以生成密码字符串.但是一行代码在不同版本的框架中给出了不同的结果.用户按下的键值的部分代码:
按下键:1.变量ascii为49.经过一些计算后,'e'和'n'的值:
e = 103,
n = 143,
Math.Pow(ascii, e) % n
Run Code Online (Sandbox Code Playgroud)
上述代码的结果:
在.NET 3.5(C#)中
Math.Pow(ascii, e) % n
Run Code Online (Sandbox Code Playgroud)
给9.0.
在.NET 4(C#)中
Math.Pow(ascii, e) % n
Run Code Online (Sandbox Code Playgroud)
给77.0.
Math.Pow() 在两个版本中给出正确(相同)的结果.
原因是什么,是否有解决方案?
在我的计算机中,此代码需要17秒(1000万次):
static void Main(string[] args) {
var sw = new Stopwatch(); sw.Start();
int r;
for (int i = 1; i <= 100000000; i++) {
for (int j = 1; j <= 10; j++) {
MyDivRem (i,j, out r);
}
}
Console.WriteLine(sw.ElapsedMilliseconds);
}
static int MyDivRem(int dividend, int divisor, out int remainder) {
int quotient = dividend / divisor;
remainder = dividend - divisor * quotient;
return quotient;
}
Run Code Online (Sandbox Code Playgroud)
而Math.DivRem需要27秒.
.NET Reflector为我提供了Math.DivRem的代码:
public static int DivRem(int a, int b, out …Run Code Online (Sandbox Code Playgroud) 今天我用C#编写了一个程序,我用%来计算一些索引...我的程序没有用,所以我调试了它,我意识到" % "不像我知道的其他程序语言那样工作.
例如:
在Python中%返回如下值:
for x in xrange (-5, 6):
print x, "% 5 =", x % 5
-5 % 5 = 0
-4 % 5 = 1
-3 % 5 = 2
-2 % 5 = 3
-1 % 5 = 4
0 % 5 = 0
1 % 5 = 1
2 % 5 = 2
3 % 5 = 3
4 % 5 = 4
5 % 5 = 0
Run Code Online (Sandbox Code Playgroud)
在C#中:
for …Run Code Online (Sandbox Code Playgroud) 在 C# 中,可以轻松获得整数除法的商和模:
var quotient = 7 / 3;
var modulus = 7 % 3;
Run Code Online (Sandbox Code Playgroud)
然而,据我所知,它没有一个divmod功能可以让你同时获得这两个功能。有没有办法做到这一点?单独获取它们似乎需要进行两次除法,这是不必要的,因为处理器肯定会同时为一次除法提供两个值。
有没有办法检查一个值是否可以被另一个数字完全整除,例如 1000 除以 100 为真,但 1115 除以 100 为假?
我正在尝试
任何帮助将非常感激 :-)