zoukankan      html  css  js  c++  java
  • UVa 424 Integer Inquiry

      大数加法,代码如下:

    View Code
      1 #include <iostream>
      2 #include <cstdio>
      3 #include <cstring>
      4 #include <algorithm>
      5 using namespace std;
      6 
      7 const int maxn = 1000;
      8 
      9 struct bign
     10 {
     11     int d[maxn];
     12     int len;
     13 
     14     bign()
     15     {
     16         memset(d, 0, sizeof(d));
     17         len = 1;
     18     }
     19 
     20     bign(int num)
     21     {
     22         *this = num;
     23     }
     24 
     25     bign(char * num)
     26     {
     27         *this = num;
     28     }
     29 
     30     bign operator = (const char * num)
     31     {
     32         len = strlen(num);
     33         for(int i = 0; i < len; i++)
     34             d[i] = num[len-1-i] - '0';
     35         return *this;
     36     }
     37 
     38     bign operator = (int num)
     39     {
     40         char s[maxn];
     41         sprintf(s, "%d", num);
     42         *this = s;
     43         return *this;
     44     }
     45 
     46     bign operator + (const bign& b) const
     47     {
     48         bign c;
     49         c.len = 0;
     50         int g = 0;
     51         for(int i = 0; i < max(len, b.len) || g; i++)
     52         {
     53             int x = g;
     54             if(i < len)   x += d[i];
     55             if(i < b.len)   x += b.d[i];
     56             c.d[c.len++] = x % 10;
     57             g = x / 10;
     58         }
     59         return c;
     60     }
     61 
     62     bign operator += (const bign& b)
     63     {
     64         *this = *this + b;
     65         return *this;
     66     }
     67 
     68     void clean()
     69     {
     70         while(len > 1 && !d[len-1])
     71             len--;
     72     }
     73     
     74     bign operator - (const bign& b)
     75     {
     76         bign c;
     77         c.len = 0;
     78         int g = 0;
     79         for(int i = 0; i < len; i++)
     80         {
     81             int x = d[i] - g;
     82             if(i < b.len)   x -= b.d[i];
     83             if(x >= 0)   g = 0;
     84             else 
     85             {
     86                 g = 1;
     87                 x += 10;
     88             }
     89             c.d[c.len++] = x;
     90         }
     91         c.clean();
     92         return c;
     93     }
     94 
     95     bign operator * (const bign& b)
     96     {
     97         bign c;
     98         c.len = len + b.len;
     99         for(int i = 0; i < len; i++)
    100             for(int j = 0; j < b.len; j++)
    101                 c.d[i+j] += d[i] * b.d[j];
    102         for(int i = 0; i < c.len-1; i++)
    103         {
    104             c.d[i+1] += c.d[i] / 10;
    105             c.d[i] %= 10;
    106         }
    107         c.clean();
    108         return c;
    109     }
    110 
    111     bool operator < (const bign& b) const
    112     {
    113         if(len != b.len)   return len < b.len;
    114         for(int i = len-1; i >= 0; i--)
    115             if(d[i] != b.d[i])   return d[i] < b.d[i];
    116         return false;
    117     }
    118 
    119     bool operator > (const bign& b) const
    120     {
    121         return b < *this;
    122     }
    123 
    124     bool operator <= (const bign& b) const
    125     {
    126         return !(b < *this);
    127     }
    128 
    129     bool operator >= (const bign& b) const
    130     {
    131         return !(*this < b);
    132     }
    133 
    134     bool operator != (const bign& b) const
    135     {
    136         return b < *this || *this < b;
    137     }
    138 
    139     bool operator == (const bign& b) const
    140     {
    141         return !(b < *this) && !(b > *this);
    142     }
    143 
    144     string str() const
    145     {
    146         string res = "";
    147         for(int i = 0; i < len; i++)
    148             res = (char)(d[i]+'0') + res;
    149         if(res == "")   res = "0";
    150         return res;
    151     }
    152 };
    153 
    154 istream& operator >> (istream &in, bign& x)
    155 {
    156     string s;
    157     in >> s;
    158     x = s.c_str();
    159     return in;
    160 }
    161 
    162 ostream& operator << (ostream & out, const bign& x)
    163 {
    164     out << x.str();
    165     return out;
    166 }
    167 
    168 int main()
    169 {
    170 #ifdef LOCAL
    171     freopen("in", "r", stdin);
    172 #endif
    173     char s[110];
    174     bign ans = 0;
    175     while(scanf("%s", s) != EOF)
    176     {
    177         if(s[0] == '0')   break;
    178         bign a = s;
    179         ans += a;
    180     }
    181     cout<<ans<<endl;
    182     return 0;
    183 }
    184         
  • 相关阅读:
    通过PROFINET网络实现SINAMICS 120的PN IO OPC通讯,起动及调速控制
    Python datetime获取当前年月日时分秒
    计算机网络:套接字(Socket)| Python socket实现服务器端与客户端通信,使用TCP socket阿里云ECS服务器与本机通信
    Ubuntu16.04安装、卸载宝塔软件
    Ubuntu一键安装LAMP,LNMP
    STM32使用K型热电偶测温:运算放大器+内置ADC+K型热电偶分度表+中间温度定律 | K型热电偶的温度-热电势曲线
    盘点几种DIY加密狗的制作方法,适用于穿越机模拟器
    变频器通讯参数PKW和PZD的含义
    穿越机从0到起飞:选件
    西门子S7-1200PLC不让下载一直报“模块具有激活的测试和调试功能,防止下载到设备”解决方法
  • 原文地址:https://www.cnblogs.com/xiaobaibuhei/p/3029173.html
Copyright © 2011-2022 走看看