高精度 大數加法 乘法 除法 冪 模板

2021-06-22 15:23:12 字數 3303 閱讀 2145

一:加法

1、普通兩個大數相加

**如下:

#include #include #include void fan(char s)

}int main()

else

p = 0;

}if(p)

z[i++] = '1';

z[i] = '\0';

fan(x);

fan(y);

fan(z);

printf("%s + %s = %s\n",x,y,z);

}return 0;

}

2、多個大數相加://poj 1503 

#include #include const int maxn = 117;

int main()

; int i, j;

while(gets(s))

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

for(i = 0; sum[i] == 0 && i < 111; i++)

}for( ; i < 111; i++)

printf("\n");

return 0;

}

二:大數乘法

1、普通模擬乘法(char )

**如下://poj 2389   ζёсяêτ - 小優you

小優you

#include #include #include #include #include using namespace std;

const int size = 1000; //大數字數

void mult(char* a,char* b,char* ans)

; int b[size+1]= ;

int pa=0,pb=0;

int c[2*size+1]= ;

int lena=strlen(a);

int lenb=strlen(b);

for(int i=lena-1; i>=0; i--)

a[pa++]=a[i]-'0';

for(int j=lenb-1; j>=0; j--)

b[pb++]=b[j]-'0';

for(pb=0; pb=0; pb--)

if(sign)

ans[pa]='\0';

else

return;

}int main()

} return s1;

}string mult(string s,int x) //大數乘以整形數

complex operator +(const complex &b)

complex operator -(const complex &b)

complex operator *(const complex &b)

};/*

* 進行fft和ifft前的反轉變換。

* 位置i和 (i二進位制反轉後位置)互換

* len必須去2的冪

*/void change(complex y,int len)

if(j < k) j += k;

}}/*

* 做fft

* len必須為2^k形式,

* on==1時是dft,on==-1時是idft

*/void fft(complex y,int len,int on)}}

if(on == -1)

for(int i = 0; i < len; i++)

y[i].r /= len;

}const int maxn = 200010;

complex x1[maxn],x2[maxn];

char str1[maxn/2],str2[maxn/2];

int sum[maxn];

int main()

len = len1+len2-1;

while(sum[len] <= 0 && len > 0)len--;

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

printf("%c",sum[i]+'0');

printf("\n");

}return 0;

}

三:大數除法

1(string)

大數除以整形數

**如下:

string except(string s,int x)  //大數除以整形數

else

}return ans;

}

四、浮點大數求冪

**如下:ζёсяêτ - 小優you

#include #include #include #include using namespace std;

const int size=1000; //大數字數

void mult(char* a,char* b,char* ans)

; int b[size+1]= ;

int pa=0,pb=0;

/*倒序*/

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

for(j=lenb-1; j>=0; j--)

int c[2*size+1]= ;

int lenc;

for(pb=0; pb=0; pb--)

ans[pa++]=c[pb]+'0';

ans[pa]='\0';

lena=pa;

/*插入小數點*/

bool flag=true; //標記是否需要刪除小數末尾的0

if(fract==0) //小數字數為0,無需插入小數點

flag=false;

else if(fract0; pa--,i++)

else

ans[pa]=ans[pa-1];}}

else //小數字數大於等於ans長度,在ans前面恰當位置插入小數點

{char temp[size+1];

strcpy(temp,ans);

ans[0]='0';

ans[1]='.';

for(int i=0; i>a>>b)

{memset(ans,'\0',sizeof(ans));

ans[0]='1';

ans[3]='\0';

for(int i=1; i<=b; i++)

mult(a,ans,ans);

cout<

ps:其餘見:

高精度加法 減法 乘法 除法

給定兩個正整數,計算它們的和。輸入格式 共兩行,每行包含乙個整數。輸出格式 共一行,包含所求的和。資料範圍 1 整數長度 100000 輸入樣例 1223 輸出樣例 35高精度加法利用陣列儲存每一位數,把最後一位儲存到陣列的第一位會方便操作一些,然後就是模擬人為加法的過程 include using...

高精度除法 大數除法

問題描述 給出兩個正整數,計算a b的值,保證a和b的位數不超過500位。輸入描述 輸入兩個用空格隔開的正整數。輸出描述 輸出a b的值。樣例輸入 500 2 樣例輸出 250 解題思路 大數除法的實現有兩種方式 1 用a b,迴圈,直到a 計算過程 擴充套件b的位數 a.length b.lent...

高精度加法和高精度乘法

題目描述 谷學長有乙個非常簡單的問題給你,給你兩個整數a和b,你的任務是計算a b。輸入 輸入的第一行包含乙個整數t t 20 表示測試例項的個數,然後2 t行,分別表示a和b兩個正整數。注意整數非常大,那意味著你不能用32位整數來處理。你可以確定的是整數的長度不超過1000。輸出 對於每乙個樣例,...