本文章介绍了高精度加减乘除,阶乘
(本人此时很懒不想写注释了,哈哈)
高精加:
#include <bits/stdc++.h>
using namespace std;
void fzh (string s,int a[])
{
for (int i=0,k=s.size()-1;i<s.size();i++,k--)
{
a[k]=s[i]-48;
}
}
void fqh (int a[],int n1,int b[],int n2,int he[],int *nn)
{
for (int i=0;i<max(n1,n2);i++)
{
he[i]=a[i]+b[i];
}
for (int i=0;i<max(n1,n2);i++)
{
he[i+1]+=he[i]/10;
he[i]%=10;
}
if (he[*nn]>0) *nn=*nn+1;
}
string fzh_(int he[],int n)
{
string s;
for (int i=n-1;i>=0;i--)
{
char c=he[i]+'0';
s=s+c;
}
return s;
}
int main (){
string s1,s2;
int a1[120]={},a2[120]={};
cin>>s1>>s2;
fzh(s1,a1);
fzh(s2,a2);
int n1=s1.size(),n2=s2.size();
int he[200]={};
int n=max(n1,n2);
fqh(a1,n1,a2,n2,he,&n);
string ans=fzh_(he,n);
cout<<ans;
return 0;
}
高精减:
#include <bits/stdc++.h>
using namespace std;
void fzh (string s,int a[])
{
for (int i=0,k=s.size()-1;i<s.size();i++,k--)
{
a[k]=s[i]-48;
}
}
void fqh (int a[],int n1,int b[],int n2,int he[],int *nn)
{
for (int i=0;i<max(n1,n2);i++)
{
he[i]=a[i]-b[i];
}
for (int i=0;i<max(n1,n2);i++)
{
if (he[i]<0)
{
he[i+1]--;
he[i]+=10;
}
}
while (he[*nn-1]==0) *nn=*nn-1;
}
string fzh_(int he[],int n)
{
string s;
for (int i=n-1;i>=0;i--)
{
char c=he[i]+'0';
s=s+c;
}
return s;
}
bool fmin (string s1,string s2)
{
if (s1.size()!=s2.size())
{
return s1.size()<s2.size();
}
else
{
return s1<s2;
}
}
int main (){
string s1,s2;
int a1[120]={},a2[120]={};
cin>>s1>>s2;
if (s1==s2)
{
cout<<0;return 0;
}
if (fmin(s1,s2))
{
swap (s1,s2);
cout<<'-';
}
fzh(s1,a1);
fzh(s2,a2);
int n1=s1.size(),n2=s2.size();
int he[200]={};
int n=max(n1,n2);
fqh(a1,n1,a2,n2,he,&n);
string ans=fzh_(he,n);
cout<<ans;
return 0;
}
高精乘:
#include<bits/stdc++.h>
using namespace std;
void fzh(string s,int a[]){
for(int i=0,k=s.size()-1;i<s.size();i++,k--){
a[k] = s[i]-48;
}
}
void fqh(int a[],int n1,int b[],int n2,int he[],int *nn){
for(int i=0;i<n1;i++){
int k=i;
for(int j=0;j<n2;j++){
he[k] += a[i]*b[j];
k++;
}
*nn=k;
for(int j=0;j<*nn;j++){
he[j+1]+=he[j]/10;
he[j]%=10;
}
}
if(he[*nn]>0) *nn=*nn+1;
}
string fzh1(int he[],int n){
string s;
for(int i=n-1;i>=0;i--){
char c = he[i]+'0';
s=s+c;
}
return s;
}
void fprint (int a[],int n){
for(int i=n-1;i>=0;i--){
cout<<a[i];
}
cout<<endl;
}
int main(){
string s1,s2;
int a1[200] = {},a2[200] = {},he[20000] = {};
cin>>s1>>s2;
fzh(s1,a1);
fzh(s2,a2);
int n = max(s1.size(),s2.size());
fqh(a1,s1.size(),a2,s2.size(),he,&n);
string ans = fzh1(he,n);
cout<<ans;
return 0;
}
高精除:
#include <bits/stdc++.h>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <queue>
#include <cmath>
using namespace std;
bool judge(int* a,int* b,int len)
{
if(a[len]>0) return true;
for(int i=len-1;i>=0;i--){
if(a[i]>b[i]) return true;
else if(a[i]<b[i]) return false;
}
return true;
}
int main(){
string a_s,b_s;
int a[521]={0},b[521]={0},ans[521]={0};
int i,j,len_a,len_b,len;
cin>>a_s>>b_s;
if(b_s=="0"){
cout<<"除数不能为0"<<endl;
return 0;
}
len_a=a_s.length();
len_b=b_s.length();
len=len_a-len_b;
for(i=0;i<len_a;i++) a[i]=a_s[len_a-1-i]-'0';
for(i=0;i<len_b;i++) b[i]=b_s[len_b-1-i]-'0';
for(i=len_a-len_b;i>=0;i--){
while(judge(a+i,b,len_b)){
ans[i]++;
for(j=0;j<=len_b-1;j++){
if(a[i+j]<b[j]){
a[i+j+1]--;
a[i+j]+=10;
}
a[i+j]-=b[j];
}
}
}
while(a[len_a]==0&&len_a>0) len_a--;
while(ans[len]==0&&len>0) len--;
for(i=len;i>=0;i--) cout<<ans[i];
if(len<0) cout<<"0";
if(len_a>1||a[0]>0){
cout<<"余";
for(i=len_a;i>=0;i--) cout<<a[i];
}
}
高精阶乘:
#include <bits/stdc++.h>
using namespace std;
int a[6000000];
void fzh (string s,int a[])
{
for (int i=0,k=s.size()-1;i<s.size();i++,k--)
{
a[k]=s[i]-48;
}
}
string fzh_(int he[],int n)
{
string s;
for (int i=n-1;i>=0;i--)
{
char c=he[i]+'0';
s=s+c;
}
return s;
}
void fjc (int a1[],int n1,int i,int ans[],int *nn)
{
for (int j=0;j<n1;j++)
{
ans[j]=a[j]*i;
}
for (int j=0;j<n1;j++)
{
ans[j+1]+=ans[j]/10;
ans[j]%=10;
}
*nn=n1;
while (ans[*nn]>0)
{
ans[*nn+1]+=ans[*nn]/10;
ans[*nn]%=10;
*nn=*nn+1;
}
}
int main (){
int n;
cin>>n;
string s="1";
fzh(s,a);
int ln=s.size();
for (int i=2;i<=n;i++)
{
fjc(a,ln,i,a,&ln);
}
s=fzh_(a,ln);
cout<<s;
return 0;
}