• 高精度算法模板


    高精度加1

     1 #include <stdio.h>
     2 #include <string.h>
     3 int main()
     4 {
     5     char a[310], b[310];
     6     int c[310], d[310], e[310];
     7     scanf("%s %s", a, b);
     8     int x = strlen(a), y = strlen(b);
     9     for (int i = 0; i < x; i++) {
    10         c[x-1-i] = a[i] - '0';
    11     }
    12     for (int i = 0; i < y; i++) {
    13         d[y-1-i] = b[i] - '0';
    14     }
    15     int l = (x > y) ? x : y;
    16     memset(e, 0, sizeof(e));
    17     for (int i = 0; i < l; i++) {
    18         e[i] += c[i] + d[i];
    19         if(e[i] > 9) e[i] -= 10,e[i+1] = 1;
    20     }
    21     if(e[l]) l++;
    22     for(int i = l-1; i >= 0; i--) printf("%d", e[i]);
    23     printf("
    ");
    24     return 0;
    25 }

    高精度减法

     1 #include <cstdio>
     2 #include <cstring>
     3 using namespace std;
     4  
     5 const int maxn = 300 + 5;
     6  
     7 int main(int argc, char **argv) {
     8     int a[maxn], b[maxn], c[maxn];
     9     char s1[maxn], s2[maxn];
    10     char *ss1 = s1, *ss2 = s2;
    11     memset(a, 0, sizeof(a) );
    12     memset(b, 0, sizeof(b) );
    13     memset(c, 0, sizeof(c) );
    14  
    15     scanf("%s%s", s1, s2);
    16     if( strlen(s1)<strlen(s2) || (strlen(s1)==strlen(s2)&&strcmp(s1,s2)<0) ){
    17         ss1 = s2;
    18         ss2 = s1;
    19         printf("-");
    20     }else if( !strcmp(s1,s2) ){
    21         printf("0");
    22         return 0;
    23     }
    24     int len1 = strlen(ss1);
    25     int len2 = strlen(ss2);
    26     for( int i=0; i<len1; i++ )
    27         a[len1-1-i] = ss1[i]-'0';
    28     for( int i=0; i<len2; i++ )
    29         b[len2-1-i] = ss2[i]-'0';
    30     int j;
    31     for( j=0; j<len1||j<len2; j++ ){
    32         c[j] += a[j]-b[j];
    33         if( c[j]<0 ){
    34             c[j] += 10;
    35             c[j+1] --;
    36         }
    37     }
    38     int lenc = j;
    39     for( ; lenc>=0 && !c[lenc]; lenc-- );
    40     for( ; lenc>=0; lenc-- )
    41         printf("%d", c[lenc]);
    42     /*scanf("%s%s", s1, s2);
    43     printf("%d", strcmp(ss1, ss2));*/
    44  
    45     return 0;
    46 }

    高精度乘法

     1 #include<cstdio>
     2 #include<cstring>
     3 using namespace std;
     4  
     5 char a1[55],b1[55];
     6 int a[55],b[55],c[125];
     7  
     8 int main(){
     9     scanf("%s%s",a1,b1);
    10     int lena=strlen(a1);
    11     int lenb=strlen(b1);
    12     for(int i=1;i<=lena;i++)
    13         a[i]=a1[lena-i]-'0';
    14     for(int i=1;i<=lenb;i++)
    15         b[i]=b1[lenb-i]-'0';
    16     for(int i=1;i<=lena;i++){
    17         int x=0;
    18         for(int j=1;j<=lenb;j++){
    19             c[i+j-1]+=a[i]*b[j]+x;
    20             x=c[i+j-1]/10;
    21             c[i+j-1]%=10;
    22         }
    23         c[i+lenb]=x;
    24     }
    25     int lenc=lena+lenb;
    26     while(c[lenc]==0&&lenc>1)
    27         lenc--;
    28     while(lenc)
    29         printf("%d",c[lenc--]);
    30     return 0;
    31 }

    高精乘除单精法

     1 #include<stdlib.h>
     2 #include<stdio.h>
     3 char s[501],ch;
     4 int a[501],b,ka,k=0;
     5 void f()
     6 {
     7      int i,c=0;
     8      for(i=0;i<k;i++)
     9      {
    10          a[i]=a[i]*b+c;
    11          if(a[i]>=10)
    12          {
    13             c=a[i];
    14             a[i]=a[i]%10;
    15             c=c/10;
    16          }else c=0;
    17      }
    18      if(c!=0) printf("%d",c);
    19      for(i=k-1;i>=0;i--)
    20      printf("%d",a[i]);
    21 }
    22 void x()
    23 {
    24      int d=0,i,c;
    25      for(i=k-1;i>=0;i--)
    26      {
    27          c=a[i]+d*10;
    28          a[i]=(a[i]+d*10)/b;
    29          d=c%b;
    30      }
    31      while((a[k-1]==0)&&(k>0)) k--;
    32      for(i=k-1;i>=0;i--)
    33       printf("%d",a[i]);
    34      if(d!=0)
    35       printf("---%d",d);
    36 }
    37 int main()
    38 {
    39     int i,temp;
    40     scanf("%s",s);
    41     while((s[k]!='*')&&(s[k]!='/'))
    42     {
    43         a[k]=s[k]-'0';
    44         k++;
    45     }
    46     ch=s[k];
    47     b=s[strlen(s)-1]-'0';
    48     ka=k;//读入
    49     for(i=0;i<(ka>>1);i++)
    50     {
    51         temp=a[i];
    52         a[i]=a[ka-1-i];
    53         a[ka-1-i]=temp;
    54     }
    55     if(ch=='*') f(); else x();
    56     system("pause");
    57     return 0;
    58 }
  • 相关阅读:
    golang 数组的一些自问自答
    这辈子研究 我的闪存
    我写的诗 爱你等于爱自己
    个人创业相关资料 创业与投资文章
    公司商业模式案例讲座 公司商业模式
    1、开篇:公司的价值 企业管理线细化系列文章
    visual studio 2022 企业版 下载
    芝奇DDR4 5066Hz 8Gx2内存闲置 个人闲置物品买卖
    INF: How SQL Server Compares Strings with Trailing Spaces
    虚拟 DOM 到底是什么?
  • 原文地址:https://www.cnblogs.com/rebirth-death2019/p/13778603.html
Copyright © 2020-2023  润新知