高精度加、减、乘笔记

zyl 喵星人 2023-10-04 9:45:12 2023-10-05 12:25:38 13
#include<iostream>
using namespace std;

// 高精度加法 string jia(string a, string b){ int lena = a.length(); int lenb = b.length(); int lenc = max(lena, lenb); int aa[2005]={0},bb[2005]={0},cc[2005]={0}; string c = ""; for(int i=0;i<lena;i++) aa[i] = a[lena-i-1]-48; for(int i=0;i<lenb;i++) bb[i] = b[lenb-i-1]-48; int x = 0; for(int i=0;i<lenc;i++){ cc[i] = aa[i] + bb[i] + x; x = cc[i]/10; cc[i] = cc[i]%10; } cc[lenc++] = x; while(cc[lenc-1]==0&&lenc-1>0) lenc--; for(int i=0;i<lenc;i++) c += char(cc[lenc-i-1]+48); return c; }

// 高精度数大小判断 bool max_a(string a, string b){ int lena = a.length(); int lenb = b.length(); if(lena!=lenb) return lena>lenb; else{ for(int i=0;i<lena;i++){ if(a[i] == b[i]) continue; return a[i]>b[i]; } } return true; }

// 高精度减法 string jian(string a, string b){ string c; if(!max_a(a,b)){ c = a; a = b; b = c; c = "-"; } int lena = a.length(); int lenb = b.length(); int lenc = max(lena, lenb); int aa[2005]={0},bb[2005]={0},cc[2005]={0}; for(int i=0;i<lena;i++) aa[i] = a[lena-i-1]-48; for(int i=0;i<lenb;i++) bb[i] = b[lenb-i-1]-48; int x = 0; for(int i=0;i<lenc;i++){ cc[i] = aa[i] - bb[i] - x; if(cc[i]<0){ cc[i] += 10; x = 1; } else x = 0; } cc[lenc++] = x; while(cc[lenc-1]==0&&lenc-1>0) lenc--; for(int i=0;i<lenc;i++) c += char(cc[lenc-i-1]+48); return c; }

// 高精度乘法 string cheng(string a, string b){ string s = ""; int aa[505] = {0}, bb[505] = {0}, cc[1005] = {0}; int lena = a.length(); int lenb = b.length(); int len = lena+lenb-1;

<span class="hl-keyword hl-control hl-c++">for</span><span class="hl-meta hl-group hl-c++"><span class="hl-punctuation hl-section hl-group hl-begin hl-c++">(</span><span class="hl-storage hl-type hl-c">int</span> i<span class="hl-keyword hl-operator hl-assignment hl-c">=</span><span class="hl-constant hl-numeric hl-c++">0</span><span class="hl-punctuation hl-terminator hl-c++">;</span>i<span class="hl-keyword hl-operator hl-comparison hl-c">&lt;</span>lena<span class="hl-punctuation hl-terminator hl-c++">;</span>i<span class="hl-keyword hl-operator hl-arithmetic hl-c">+</span><span class="hl-keyword hl-operator hl-arithmetic hl-c">+</span><span class="hl-punctuation hl-section hl-group hl-end hl-c++">)</span></span><span class="hl-meta hl-block hl-c++"><span class="hl-punctuation hl-section hl-block hl-begin hl-c++">{</span>
	aa<span class="hl-meta hl-brackets hl-c++"><span class="hl-punctuation hl-section hl-brackets hl-begin hl-c++">[</span>i<span class="hl-punctuation hl-section hl-brackets hl-end hl-c++">]</span></span> <span class="hl-keyword hl-operator hl-assignment hl-c">=</span> a<span class="hl-meta hl-brackets hl-c++"><span class="hl-punctuation hl-section hl-brackets hl-begin hl-c++">[</span>lena<span class="hl-keyword hl-operator hl-arithmetic hl-c">-</span><span class="hl-constant hl-numeric hl-c++">1</span><span class="hl-keyword hl-operator hl-arithmetic hl-c">-</span>i<span class="hl-punctuation hl-section hl-brackets hl-end hl-c++">]</span></span> <span class="hl-keyword hl-operator hl-arithmetic hl-c">-</span> <span class="hl-constant hl-numeric hl-c++">48</span><span class="hl-punctuation hl-terminator hl-c++">;</span>
<span class="hl-punctuation hl-section hl-block hl-end hl-c++">}</span></span>
<span class="hl-keyword hl-control hl-c++">for</span><span class="hl-meta hl-group hl-c++"><span class="hl-punctuation hl-section hl-group hl-begin hl-c++">(</span><span class="hl-storage hl-type hl-c">int</span> i<span class="hl-keyword hl-operator hl-assignment hl-c">=</span><span class="hl-constant hl-numeric hl-c++">0</span><span class="hl-punctuation hl-terminator hl-c++">;</span>i<span class="hl-keyword hl-operator hl-comparison hl-c">&lt;</span>lenb<span class="hl-punctuation hl-terminator hl-c++">;</span>i<span class="hl-keyword hl-operator hl-arithmetic hl-c">+</span><span class="hl-keyword hl-operator hl-arithmetic hl-c">+</span><span class="hl-punctuation hl-section hl-group hl-end hl-c++">)</span></span><span class="hl-meta hl-block hl-c++"><span class="hl-punctuation hl-section hl-block hl-begin hl-c++">{</span>
	bb<span class="hl-meta hl-brackets hl-c++"><span class="hl-punctuation hl-section hl-brackets hl-begin hl-c++">[</span>i<span class="hl-punctuation hl-section hl-brackets hl-end hl-c++">]</span></span> <span class="hl-keyword hl-operator hl-assignment hl-c">=</span> b<span class="hl-meta hl-brackets hl-c++"><span class="hl-punctuation hl-section hl-brackets hl-begin hl-c++">[</span>lenb<span class="hl-keyword hl-operator hl-arithmetic hl-c">-</span><span class="hl-constant hl-numeric hl-c++">1</span><span class="hl-keyword hl-operator hl-arithmetic hl-c">-</span>i<span class="hl-punctuation hl-section hl-brackets hl-end hl-c++">]</span></span> <span class="hl-keyword hl-operator hl-arithmetic hl-c">-</span> <span class="hl-constant hl-numeric hl-c++">48</span><span class="hl-punctuation hl-terminator hl-c++">;</span>
<span class="hl-punctuation hl-section hl-block hl-end hl-c++">}</span></span>
<span class="hl-comment hl-line hl-double-slash hl-c"><span class="hl-punctuation hl-definition hl-comment hl-c">//</span> 乘法核心

for(int i=0;i<lena;i++){ for(int j=0;j<lenb;j++){ cc[i+j] += aa[i]*bb[j]; cc[i+j+1] += cc[i+j]/10; cc[i+j] %= 10; if(cc[len]) len++; } } // 防止结果为零 while(cc[len-1] == 0 && len-1>0) len--; for(int i=len-1;i>=0;i--){ s += char(cc[i]+48); } return s; }

int main(){ string a,b,c; cin>>a>>b>>c; cout<<jian(jia(a,b),c); return 0; }

{{ vote && vote.total.up }}