大數(高精度)運算

2021-08-08 15:17:13 字數 902 閱讀 2903

在數**算中,經常會碰到這樣的問題:參與運算的資料很大或者對運算結果的精度要求很高。而在計算機語言中,描述資料的資料型別的位元組數是固定的,因此其有大小和精度的限制。例如在c/c++語言中,整型int(32位)的範圍在-2^31~2^31-1,即-2,147,483,648~2,147,483,647,單精度浮點數float(32位)的精度在小數點後6~7位。

那麼,我們要計算諸如2的100次方或者1234567890123456789*9876543210987654321,該怎麼辦呢?

那就需要使用到高精度的數**算了。

const int max = 1000;

void add(char str1,char str2)

} for(i=max-1; (i>=0)&&(a[i]==0); i--);

if(i>=0)

for(; i>=0; i--)

printf("%d",a[i]);

else

printf("0");

printf("\n");

}

(1)      當被減數小於減數,正常減

(2)      當被減數大於減數,反過來減,最後取相反數

和大數加法類似,加法採用進製,減法採用借位。

void multi(char str1,char str2)

for(i=max*2; (i>=0)&&(c[i]==0); i--);

if(i>=0)

for(; i>=0; i--)

printf("%d",c[i]);

else

printf("0");

printf("\n");

}

將除法運算轉換成減法運算

高精度運算(大數)

求模 mod 直接在草稿紙上用小學方法算除法就能看出來 1 include2 include 3char m 1010 4 int main 10 printf d n temp 11 12return0 13 大數階乘 模板。理解就好。1 include2 include 3 define n 1...

大數運算(高精度運算)

給定兩個正整數,計算它們的和。輸入格式 共兩行,每行包含乙個整數。輸出格式 共一行,包含所求的和。資料範圍 1 整數長度 100000 輸入樣例 1223 輸出樣例 35 include include include using namespace std typedef long long ll...

高精度運算(大數加法)

在計算過大的數字時,我們可以使用字串進行儲存,再模擬計算過程,結果也用字串儲存,最後輸出這個字串 加法 include include include includeusing namespace std int main l1 l2 int g 0 while l2 0 while l1 0 if...