Uva 10106 高精度,大數乘

2022-05-24 16:33:06 字數 1608 閱讀 6131

2014-06-01 18:03:30

題意 & 思路:大數乘,這裡採用的小白書裡的bign結構體

#include #include 

#include

#include

using

namespace

std;

const

int maxn = 1000

;struct

bign

bign

operator = (const

char *num)

bign

operator = (int

num)

bign(

intnum)

bign(

const

char *num)

//bign的str型轉化

string str() const

//高精度加

bign operator + (const bign & b) const

return

sum;

}//高精度乘,實際上加和乘對進製的處理有所不同,比較喜歡乘的

bign operator * (const bign &b) const

}pro.len = len + b.len + 1;//

這裡注意pro.len初始值可能是題目數字範圍兩倍

while(pro.s[pro.len - 1] == 0 && pro.len > 1

) --pro.len;//

最後一位不管是不是0都不能讓len - 1

if(pro.s[pro.len])

++pro.len;//

這句有待商討

return

pro;

}//額外加的+=過載

bign operator += (const bign &b)

//額外的比較運算子過載

bool

operator

< (const bign & b) const

bool

operator > (const bign &b) const

bool

operator

<= (const bign &b) const

bool

operator >= (const bign &b) const

bool

operator != (const bign &b) const

bool

operator == (const bign &b) const

friend istream & operator >> (istream & in,bign &x);

friend ostream & operator

<< (ostream & out,const bign &x);

};istream & operator >> (istream & in,bign &x)

ostream & operator

<< (ostream & out,const bign &x)

intmain()

return0;

}

大數相乘「高精度乘低精度」和「高精度乘高精度」

二 高精度乘高精度 如下 由於計算機的儲存位元組有限,所以不能完整表示乙個很大整數的精確值,這時候就得用到其他的方法,稱之為高精度演算法。這裡的高精度乘法主要指按位模擬乘法,實際上就是模擬乘法的過程,也就是筆算的過程。高精度乘低精度,即乙個大數與乙個小於10000的數相乘,大數使用字串來進行儲存,較...

高精度之大數乘大數

現在我們來說一下大數乘以大數。大數乘以大數也是用來模擬手算。舉個例子吧!先從個位開始乙個乙個的乘 乘完個位然後再乘十位,乘十位的時候要和個位的想成的結果相加。這裡注意乘十位的時候 就不要和乘個位數字的結果中的最後一位相加了 就是如圖搓位。就是這樣 下面先貼上我的 include includeusi...

高精度乘高精度

c a b a b均是高精度數 比如a 156,b 3,求a b 很容易知道答案是468,怎樣算的呢?首先讓3 6 18,然後向進一位,並且這位只保留8 讓3 5 進製1 16,然後向前進一位,並且這位只保留6 讓3 1 進製1 4,此時不向前進製,保留4 所以最終答案是468 通過這個例子我們再來...