【高精度算法】

洛谷上写数楼梯问题,怎么都过不了,因此知道了高精度算法。
int 数量级 1e9
long long 数量级 1e18
unsighed long long 数量级1e19

高精度加法

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
string x,y;
int a[N],b[N],c[N],la,lb,lc;
int main()
{
   cin>>x>>y;
   
   la=x.length();
   lb=y.length();
  
   for(int i=0;i<la;i++) a[la-i]=x[i]-'0';
   for(int i=0;i<lb;i++) b[lb-i]=y[i]-'0';
   
   lc=max(la,lb);
   for(int i=1;i<=lc;i++)
   {
           c[i]+=a[i]+b[i];
           c[i+1]+=c[i]/10;
           c[i]=c[i]%10;
   } 
   if(c[lc+1]>0) lc++;//进位 
   
   for(int i=lc;i>=1;i--) printf("%d",c[i]);
   return 0;
}

数楼梯

P1255 数楼梯 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

#include <bits/stdc++.h>
using namespace std;
int n,f[5010][5010],len;
void jiafa(int k)//高精加法
{
    for(int i=1; i<=len; i++)//两数相加 逐位相加
        f[k][i]=f[k-1][i]+f[k-2][i];
    for(int i=1; i<=len; i++)//进位
        if(f[k][i]>=10)
        {
            f[k][i+1]+=f[k][i]/10;
            f[k][i]%=10;
            if(f[k][len+1]>0)len++;
        }
}
signed main()
{
    cin>>n;
    len=1;
    f[1][1]=1;//预处理
    f[2][1]=2;//预处理
    for(int i=3; i<=n; i++)//开始计算
        jiafa(i);
    for(int i=len; i>=1; i--)//输出
        cout<<f[n][i];
    return 0;
}

高精度减法

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
string x,y;
int a[N],b[N],c[N],la,lb,lc;
int main()
{
   cin>>x>>y;
   la=x.length();//a,x比较大 
   lb=y.length();
   if(la<lb||(la==lb&&x<y))//如果x比较小 
   {
           swap(x,y);
           swap(la,lb);
           cout<<"-";
   }

   for(int i=0;i<la;i++) a[la-i]=x[i]-'0';
   for(int i=0;i<lb;i++) b[lb-i]=y[i]-'0';
   
   for(int i=1;i<=la;i++)
   {
           if(a[i]<b[i])
           {
               a[i]+=10;
               a[i+1]--;
        }
        c[i]=a[i]-b[i];
   } 
   while(c[la]==0&&la!=1) la--;//删除前导0 
   
   for(int i=la;i>=1;i--) printf("%d",c[i]);
   return 0;
}

高精度乘法

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
string x,y;
int a[N],b[N],c[N],la,lb,lc;
int main()
{
   cin>>x>>y;
   la=x.length(); 
   lb=y.length();

   for(int i=0;i<la;i++) a[la-i]=x[i]-'0';
   for(int i=0;i<lb;i++) b[lb-i]=y[i]-'0';
   
   for(int i=1;i<=la;i++)
   {
           for(int j=1;j<=lb;j++)
           {
               c[i+j-1]+=a[i]*b[j];
               c[i+j]+=c[i+j-1]/10;//进位 
               c[i+j-1]%=10;
        }
   } 
   lc=la+lb;
   while(c[lc]==0&&lc!=1) lc--;//删除前导0 
   
   for(int i=lc;i>=1;i--) printf("%d",c[i]);
   return 0;
}

高精度除法

高精度除以低精度(大数/小数

P1480 A/B Problem - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)

逐位试商法:

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;
string s;
long long b,c[N],a[N],x,la,lc;
int main()
{
   cin>>s>>b;
   la=s.length(); 

   for(int i=1;i<=la;i++) a[i]=s[i-1]-'0';

   for(int i=1;i<=la;i++)
   {
           c[i]=(x*10+a[i])/b;
           x=(x*10+a[i])%b;
   } 
   lc=1;
   while(c[lc]==0&&lc<la) lc++;//删除前导0 
   
   for(int i=lc;i<=la;++i) printf("%d",c[i]);
   return 0;
}

高精度除以高精度

减法模拟除法:

埋坑。。。

猜你喜欢

转载自blog.csdn.net/m0_74183164/article/details/129772555