• 结构体封装高精度 大整数BigInt


    曾经很讨厌高精度,因为它很长,不好记,而且在不是很单纯的题目里面感觉很烦(一个数就是一个数组)。在一道题目中出现的时候总是用一些奇技淫巧混过去(比如把两个$long$ $long$拼在一起)。

    现在...还是正视了这个问题,有时候该写还是要写的(毕竟联赛不能用$_int128$什么的)抽空把它搞成了一个结构体封装的形式,看起来要清爽一些。

     1 #include<cstdio>
     2 #include<algorithm>
     3 #include<vector>
     4 #include<cstring>
     5 using namespace std;
     6 #define ML 505//MaxLenth
     7 #define ll long long
     8 #define INF 0x3f3f3f3f
     9 struct BT//BigInt
    10 {
    11     int a[ML],len;
    12     BT()//初始化
    13     {
    14         memset(a,0,sizeof(a));
    15         len=1;
    16     } 
    17     BT operator + (const BT &A)const
    18     {
    19         BT B;
    20         B.len=max(len,A.len);
    21         for(int i=0;i<B.len;i++)
    22         {
    23             B.a[i]+=A.a[i]+a[i];
    24             if(B.a[i]>=10)
    25             {//进位 9+9=18 进位不会超过10 
    26                 B.a[i]-=10;
    27                 B.a[i+1]++;
    28             }
    29         }
    30         if(B.a[B.len])//进到了下一位
    31             B.len++; 
    32         return B;
    33     }
    34     void read()
    35     {
    36         char d[ML];
    37         scanf("%s",d);
    38         int l=strlen(d);
    39         for(int i=0;i<l;i++)
    40             a[i]=d[l-i-1]-'0';
    41         len=l;
    42     }
    43     void write()
    44     {
    45         for(int i=len-1;i>=0;i--)
    46             printf("%d",a[i]);
    47     }
    48 };
    49 BT S,G,L;
    50 int main() 
    51 {
    52     S.read();
    53     G.read();
    54     L=S+G;
    55     L.write();
    56     return 0; 
    57 }
    Code

    参考:

    https://blog.csdn.net/Rocky_Selene/article/details/52736357

    https://blog.csdn.net/lxp20011125/article/details/82085221

  • 相关阅读:
    .Net几大优点
    小谈.NET下的缓存。
    ASP.NET 防盗链的实现[HttpHandler]
    给网页加个个性的图标
    四个字节整型转换为IP格式
    在 ASP.NET MVC 中使用带后缀的 URL
    Visual Studio 2008 安装 SP1 后智能提示变英语的修补程序 (KB957507)
    Visual Studio 2008 SP1 安装失败的解决方法
    关于0x0d和0x0a回车换行\r和\n
    图像处理工程师的要求
  • 原文地址:https://www.cnblogs.com/lyttt/p/11805335.html
Copyright © 2020-2023  润新知