📄 program.cs
字号:
using System;
using System.Collections;
using System.Diagnostics;
namespace IBMS.Algorithem
{
/// <summary>
/// N2Class
/// </summary>
class N2
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
N2 newObject = new N2();
int baseNum = 4;
int testN = 10000;
Console.WriteLine("Std Math Lib Caculating result:" + Math.Pow(baseNum, testN).ToString());
Console.WriteLine("Wait...now begin to caculate..");
int[] result = newObject.Power(baseNum, testN);
Console.WriteLine("Caculate Result:");
for (int i = result.Length - 1; i >= 0; i--)
{
Console.Write(result[i]);
}
Console.WriteLine("");
}
int[] Power(int baseNum, int n)
{
int[] result;
if (n < 0)
{
throw new Exception("现在还不支持负指数");
}
if (n == 0)
{
result = new int[1];
result[0] = 1;
return result;
}
//获得结果位数
int size = (int)(n * Math.Log10(baseNum)) + 1;
result = new int[size];
result[0] = 1;
for (int i = 0; i < n; i++)
{
Multi_Vector(result, baseNum);
//Console.WriteLine("Caculate {0}'s {1} ",baseNum,i+1);
}
return result;
}
void CheckCarray(int[] m, int index)
{
if (m[index] >= 10)
{
//向高位进位,模拟手算
m[index + 1] += m[index] / 10;
//设置当前位
m[index] %= 10;
//检查下一个位时候需要进位
CheckCarray(m, index + 1);
}
}
/// <summary>
/// 矢量和一个整数的乘法运算
/// </summary>
/// <param name="m">矢量</param>
/// <param name="n">乘数</param>
void Multi_Vector(int[] m, int n)
{
int totalBit = 0;
for (int i = m.Length - 1; i >= 0; i--)
{
if (m[i] > 0)
{
totalBit = i + 1; //获得最高位
break;
}
}
Debug.Assert(totalBit != 0);
for (int i = totalBit - 1; i >= 0; i--)
{
//从高位到低位开始算
int temp = m[i] * n;
//产生一个进位
if (temp >= 10)
{
//向高位进位,模拟手算
m[i + 1] += temp / 10;
//检查该位是否还需要进位
CheckCarray(m, i + 1);
temp %= 10;
}
m[i] = temp;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -