zoukankan      html  css  js  c++  java
  • 【模板】高精度

    我之前一直觉得高精度很麻烦,然而今天一打才发现

    这么个zz东西我居然还弃疗过(模板果然还是要自己打)

    高精度加法

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<algorithm>
     5 using namespace std;
     6 int lena,lenb,tot;
     7 char a[505],b[505];
     8 int aa[505],bb[505],c[505],x;
     9 void inti()
    10 {
    11     cin>>a;cin>>b; 
    12 }
    13 void pre()
    14 {
    15      lena=strlen(a)-1;lenb=strlen(b)-1;
    16      for(int i=0;i<=lena;i++) aa[lena-i]=a[i]-'0';
    17      for(int i=0;i<=lenb;i++) bb[lenb-i]=b[i]-'0';
    18 }
    19 void work()
    20 {
    21     tot=max(lena,lenb);
    22     for(int i=0;i<=tot;i++)
    23     {
    24         c[i]=aa[i]+bb[i]+x;
    25         x=c[i]/10;c[i]%=10;
    26     }
    27     if(x>0) c[++tot]=x;
    28 }
    29 void pri()
    30 {
    31     while(tot>=0)
    32     {
    33         cout<<c[tot];
    34         tot--;
    35     }
    36 }
    37 int main()
    38 {
    39     inti();
    40     pre();
    41     work();
    42     pri();
    43     return 0;
    44 }
    高精度加法

    高精度减法

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<algorithm>
     5 using namespace std;
     6 int lena,lenb,tot;
     7 string a,b,d;
     8 int aa[505],bb[505],c[505],x;
     9 void inti()
    10 {
    11     cin>>a;cin>>b; 
    12     if((a.size()<b.size())||(a.size()==b.size()&&a<b))
    13     {
    14        cout<<"-";
    15        d=a;a=b;b=d;
    16     }
    17 }
    18 void pre()
    19 {
    20      lena=a.size()-1;lenb=b.size()-1;
    21      for(int i=0;i<=lena;i++) aa[lena-i]=a[i]-'0';
    22      for(int i=0;i<=lenb;i++) bb[lenb-i]=b[i]-'0';
    23 }
    24 void work()
    25 {
    26     tot=max(lena,lenb);
    27     for(int i=0;i<=tot;i++)
    28     {
    29         c[i]=aa[i]-bb[i]-x;
    30         if(c[i]<0) c[i]+=10,x=1;
    31         else x=0;
    32     }
    33 }
    34 void pri()
    35 {
    36     while(c[tot]==0&&tot>0) tot--;
    37     while(tot>=0)
    38     {
    39         cout<<c[tot];
    40         tot--;
    41     }
    42 }
    43 int main()
    44 {
    45     inti();
    46     pre();
    47     work();
    48     pri();
    49     return 0;
    50 }
    高精度减法

    高精度乘法

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<algorithm>
     5 using namespace std;
     6 int lena,lenb,tot;
     7 char a[505],b[505];
     8 int aa[505],bb[505],c[505],x;
     9 void inti()
    10 {
    11     cin>>a;cin>>b; 
    12 }
    13 void pre()
    14 {
    15      lena=strlen(a)-1;lenb=strlen(b)-1;
    16      for(int i=0;i<=lena;i++) aa[lena-i]=a[i]-'0';
    17      for(int i=0;i<=lenb;i++) bb[lenb-i]=b[i]-'0';
    18 }
    19 void work()
    20 {    
    21     for(int i=0;i<=lena;i++)
    22     for(int j=0;j<=lenb;j++)
    23     c[i+j]+=aa[i]*bb[j]; 
    24     tot=lena+lenb;
    25     for(int i=0;i<=tot;i++)
    26     {
    27         c[i]+=x;
    28         x=c[i]/10;
    29         c[i]=c[i]%10;  
    30     }
    31     while(x>0)
    32     {
    33         c[++tot]=x%10;
    34         x=x/10;
    35     }
    36 }
    37 void pri()
    38 {
    39     while(tot>=0)
    40     {
    41         cout<<c[tot];
    42         tot--;
    43     }
    44 }
    45 int main()
    46 {
    47     inti();
    48     pre();
    49     work();
    50     pri();
    51     return 0;
    52 }
    高精度乘法

    以上By hahaCarrot 。转载请注明出处  http://www.cnblogs.com/LQ-double/

  • 相关阅读:
    编程算法
    JDBC连接MySQL数据库及演示样例
    CKEditor&ckfindtor
    从零開始开发Android版2048 (五) 撤销的实现
    TCP/IP之分层
    poj2239 Selecting Courses --- 二分图最大匹配
    英尺到米的换算
    概率dp ZOJ 3640
    Android应用程序注冊广播接收器(registerReceiver)的过程分析
    TsFltMgr.sys系统蓝屏的原因就在于QQ电脑管家!
  • 原文地址:https://www.cnblogs.com/LQ-double/p/6067409.html
Copyright © 2011-2022 走看看