解决方案 »
- 想实现一个控件拖动排序的功能,想不通啊。。。
- XPTable 单元格怎么取值啊
- 求一思路:打开一个网站的时候在没打开之前显示 请稍候。。。
- 请问谁用过SharpMap
- 新手,怎么传递Font类型的值
- 在三层结构中,SQL语句应该出现在何处啊,有点纳闷,是不是都应该用储存过程???
- 请教C#WinForm如何调用word程序?
- 向word里面插入多张图片都叠在一起了怎么解决
- 请问如何获得winform中datagrid中某个选择列的名称或标题啊
- 请君给一个文件发送接收的原代码!谢谢!
- 请问大家开发过智能分客户端吗 ??
- C#中定义结构,结构中也可以定义函数,这个结构类型与“类”很有相似之处呀?是不是完全可以用类来替代呀?
using System.Collections.Generic;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int m = 79;
int e = 51;
int n = 90; Console.WriteLine("Result = " + ExpMod(m, e, n));
Console.ReadLine();
} public static int ExpMod(int m, int e, int n) // ((m^e) mod n)
{
List<bool> split = SplitExp(e);
if (split.Count == 0)
{
return 1;
} int result = -1;
int mod = 1;
for (int i = 0, j=0; i < split.Count; i++, j++)
{
if (j == 0)
{
mod = Mod(m, n);
}
else
{
mod = MultiMod(mod, mod, n); // (m^(2^j)) mod n
}
Console.WriteLine(string.Format("({0}^(2^{1})) mod {2} = {3}", m, j, n, mod)); if (split[i])
{
if (result == -1)
{
result = mod;
}
else
{
result = MultiMod(result, mod, n);
}
}
//Console.WriteLine("by now, result is " + result);
}
return result;
} private static List<bool> SplitExp(int e)
{
if (e <= 0)
{
return new List<bool>();
}
List<bool> bits = new List<bool>();
do
{
if (Mod(e, 2) != 0)
{
bits.Add(true);
}
else
{
bits.Add(false);
}
e = e >> 1;
} while (e != 0);
return bits;
} private static int MultiMod(int m1, int m2, int n) // (m1 * m2) mod n
{
return Mod(Mod(m1, n) * Mod(m2, n), n);
} private static int Mod(int m, int n)
{
return m % n;
}
}
}
显示结果如下:
(79^(2^0)) mod 90 = 79
(79^(2^1)) mod 90 = 31
(79^(2^2)) mod 90 = 61
(79^(2^3)) mod 90 = 31
(79^(2^4)) mod 90 = 61
(79^(2^5)) mod 90 = 31
Result = 19
{
if (m % n == 0)
{
return 0;
} if (e == 1)
{
return m % n;
} return ((m % n) * fun(m, e - 1, n)) % n;
}