加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

大整数类

发布时间:2020-12-14 04:27:24 所属栏目:大数据 来源:网络整理
导读:1 #includebits/stdc++.h 2 using namespace std; 3 const int maxn= 10000 ; // 最大处理位数 4 struct bign 5 { 6 int d[maxn],len; // 下标从0开始 7 inline void clean() { while (len 1 !d[len- 1 ]) len--;}; // 删除前导0 8 inline bign(){memset(d,
  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 const int maxn=10000;//最大处理位数
  4 struct bign
  5 {
  6     int d[maxn],len; //下标从0开始 
  7     inline void clean() {while(len>1&&!d[len-1]) len--;}; //删除前导0 
  8     inline bign(){memset(d,0,sizeof(d)),len=1;}; //构造函数 
  9     inline bign(int num) {*this=num;} //使bign类型能与int类型运算 
 10     inline bign(char* num) {*this=num;} 
 11     inline bign operator = (const char* num)
 12     {
 13         memset(d,sizeof(d));
 14         len=strlen(num);
 15         for(int i=0;i<len;i++) d[i]=num[len-1-i]-48; //反序输入 
 16         return *this;
 17     }
 18     inline bign operator = (int num)
 19     {
 20         char s[20];
 21         sprintf(s,"%d",num);
 22         *this=s;
 23         return *this;
 24     }
 25     inline bign operator + (const bign &b) //加数为非负整数 
 26     {
 27         int x=0,i,lenmax=max(len,b.len); //x用于存放进位,lenmax为位数 
 28         bign c;c.len=lenmax;
 29         for(i=0;i<lenmax;i++)
 30         {
 31             c.d[i]=d[i]+x+b.d[i];
 32             x=c.d[i]/10;
 33             c.d[i]%=10;
 34         }
 35         x?c.d[i]=x:i--; //最高位进位 
 36         c.len=i+1;        //位数加一 
 37         return c;
 38     }
 39     inline bign operator - (const bign &b) //减数不大于被减数 
 40     {
 41         bign c=*this;
 42         for(int i=0;i<c.len;i++)
 43         {
 44             c.d[i]-=b.d[i];
 45             if(c.d[i]<0) {c.d[i]+=10;c.d[i+1]--;};
 46         }
 47         c.clean();
 48         return c;
 49     }
 50     inline bign operator * (const bign &b)  const//非负整数相乘  const表示该成员函数不改变数据成员
 51                                                  //去掉const除法编译出错 
 52     {
 53         bign c;
 54         c.len=len+b.len;
 55         for(int i=0;i<len;i++)
 56         {
 57             int x=0; //用于存放进位 
 58             for(int j=0;j<b.len;j++)
 59             {
 60                 c.d[i+j]+=x+d[i]*b.d[j];
 61                 x=c.d[i+j]/10;
 62                 c.d[i+j]%=10;
 63             }
 64             c.d[i+b.len]=x; //最高位进位
 65         }
 66         c.clean();
 67         return c; 
 68     }
 69     inline bign operator / (int b) //非负整数相除(高精除低精) 
 70     {
 71         int x=0;
 72         bign c=*this;
 73         for(int i=len-1;i>=0;i--)
 74         {
 75             x=x*10+d[i];
 76             c.d[i]=x/b;
 77             x%=b;
 78         }
 79         c.clean();
 80         return c;
 81     }
 82     inline bign operator / (const bign &b)  //非负整数相除(高精除高精) 
 83     {
 84         int i,j;
 85         bign c=*this,a=0;
 86         for (i=len - 1; i >=0; i--)
 87         {
 88             a=a*10 + d[i];
 89             for (j=0; j<10; j++) if (a<b*(j+1)) break;
 90             c.d[i]=j;
 91             a=a - b*j;
 92         }
 93         c.clean();
 94         return c;
 95     } 
 96     inline bign operator % (int b) //非负整数取模(高精模低精) 
 97     {
 98         int x=0;
 99         for(int i=len-1;i>=0;i--)
100         {
101             x=x*10+d[i];
102             d[i]=x/b;
103             x%=b;
104         }
105         clean();
106         return x;
107     }
108     inline bign operator % (const bign &b) //非负整数取模(高精模高精) 
109     {
110         int i,j;
111         bign x;
112         for(i=len-1;i>=0;i--)
113         {
114             x=x*10+d[i];
115             for(j=0;j<10;j++) if(x<b*(j+1)) break;
116             x=x-b*j;
117         }
118         return x;
119     }
120     inline bool operator < (const bign &b)  const//逻辑运算符  去掉const编译错误 
121     {
122         if(len!=b.len) return len<b.len;
123         for(int i=len-1;i>=0;i--) if(d[i]!=b.d[i]) return d[i]<b.d[i];
124         return false;
125     }
126     inline bool operator > (const bign &b) const {return b<*this;}
127     inline bool operator == (const bign &b) const {return !(b<*this)&&!(*this<b);}
128     inline bool operator != (const bign &b) const {return b<*this||*this<b;}
129     inline bool operator >= (const bign &b) const {return !(*this<b);}
130     inline bool operator <= (const bign &b) const {return !(*this>b);}
131     inline string str() const //字符串转换 去掉const输出流编译错误 
132     {
133         char s[maxn]={};
134         for(int i=0;i<len;i++) s[len-i-1]=d[i]+0;
135         return s;
136     }
137 };
138 inline istream& operator >> (istream &in,bign &x) //输入流  加上const编译错误 
139 {
140     string s;
141     in>>s;
142     x=s.c_str();
143     return in;
144 }
145 inline ostream& operator << (ostream &out,const bign &x) //输出流 去掉const编译错误 
146 {
147     out<<x.str();
148     return out; 
149 }
150 int main()
151 {
152     bign a;
153     int b; 
154     cin>>a>>b;
155     cout<<a+b;
156 }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读