zoukankan      html  css  js  c++  java
  • 结构体封装高精度 大整数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

  • 相关阅读:
    [转]读取并修改App.config文件
    [转]线程和进程的概念
    实习日志(3)
    实习日志2
    实习小感,回学校啦~~~~
    请教LUA高手一段代码,希望帮忙谢谢!
    实习的日子
    vs显示 error LNK2019: 无法解析的外部符号 _main解决办法
    创建一个新窗口进程并返回进程ID号和进程的主线程ID号
    显示基本图形界面第一天
  • 原文地址:https://www.cnblogs.com/lyttt/p/11805335.html
Copyright © 2011-2022 走看看