首页 > 开发 > 综合 > 正文

超大正整数加法与乘法最笨方法实现

2024-07-21 02:22:48
字体:
来源:转载
供稿:网友
using system.text;

private string addnumber(string numa, string numb)
{
int lena=0, lenb=0;
int leni=0, intten=0;
int inta=0, intb=0;

stringbuilder result=new stringbuilder("");
lena=numa.length;
lenb=numb.length;
if(lena>lenb)
leni=lena;
else
leni=lenb;
for(int i=0; i<leni; i++)
{
if(i<lena)
inta=convert.toint16(numa.substring(lena-i-1,1));
else
inta=0;
if(i<lenb)
intb=convert.toint16(numb.substring(lenb-i-1,1));
else
intb=0;
int temp=inta+intb+intten;
result.insert(0,convert.tostring((int)temp%10));
intten=(int)temp /10;
}
if(intten>0)
result.insert(0,convert.tostring(intten));
return result.tostring();
}

private string multinum(string numa, string numb)
{
int lena=0, lenb=0;
int leni=0;

string result="0";
string addresult="0";
int dig=0;

lena=numa.length;
lenb=numb.length;
if(lena>lenb)
{
leni=lenb;
for(int i=0; i<leni; i++)
{
addresult="0";
dig=convert.toint16(numb.substring(lenb-i-1,1));
for(int j=0; j<dig; j++)
addresult=addnumber(numa,addresult);
for(int k=0; k<i; k++)
addresult=addresult + "0";
result=addnumber(addresult,result);
}
}
else
{
leni=lena;
for(int i=0; i<leni; i++)
{
addresult="0";
dig=convert.toint16(numa.substring(lena-i-1,1));
for(int j=0; j<dig; j++)
addresult=addnumber(numb,addresult);
for(int k=0; k<i; k++)
addresult=addresult + "0";
result=addnumber(addresult,result);
}
}
return result;
}

private void button2_click(object sender, system.eventargs e)
{
datetime dtm1=datetime.now;
console.writeline(addnumber("1225225236523456889","1255625622452552"));
console.writeline(dtm1.millisecond + " " + datetime.now.millisecond );
}

private void button3_click(object sender, system.eventargs e)
{
datetime dtm1=datetime.now;
string test="1";
test=multinum("453123123156341231651231563121563151213513422","1254564231541321541385435453453124563131541534313543");
console.writeline(test);
console.writeline(dtm1.millisecond + " " + datetime.now.millisecond );
}


最大的网站源码资源下载站,

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表