高精度 <加减乘除>
来源:互联网 发布:js替换标签 编辑:程序博客网 时间:2024/05/22 04:34
// by spli#include<cstring>#include<cstdio>#include<algorithm>#include<iostream>using namespace std;const int N=10010;const int p=4;const int base=10000;struct node{ char s[N]; int a[N]; void cl(){memset(a,0,sizeof(a));} void update(char *t){ memset(a,0,sizeof(a)); int len=strlen(t); reverse(t,t+len); a[0]=(p+len-1)/p; for(int i=0,c=0,w;i<len;++i,w*=10){ if(i%p==0) c++,w=1; a[c]+=w*(t[i]-'0'); } //for(int i=1;i<=a[0];++i) cout<<a[i];cout<<endl; } void pushback(int x){a[++a[0]]=x;} void rev(){reverse(a+1,a+1+a[0]);} bool operator < (const node &b)const{ //reverse后的比较 if(a[0]!=b.a[0]) return a[0]<b.a[0]; for(int i=a[0];i>=1;--i){ if(a[i]!=b.a[i]) return a[i]<b.a[i]; } return 0; } node operator + (const node &b)const{ node ret; ret.cl(); ret.a[0]=max(a[0],b.a[0]); for(int i=1;i<=ret.a[0];++i){ ret.a[i]+=a[i]+b.a[i]; ret.a[i+1]+=ret.a[i]/base; ret.a[i]%=base; } if(ret.a[ret.a[0]+1]) ret.a[0]++; //reverse(ret.a+1,ret.a+1+ret.a[0]); return ret; } node operator - (const node &b)const{ node ret; for(int i=1;i<=a[0];++i) ret.a[i]=a[i]; ret.a[0]=a[0]; for(int i=1;i<=ret.a[0];++i){ ret.a[i]-=b.a[i]; if(ret.a[i]<0) ret.a[i]+=base,ret.a[i+1]-=1; } while(ret.a[0]>0&&ret.a[ret.a[0]]==0) ret.a[0]--; return ret; } node operator * (const node &b)const{ node ret; ret.cl(); ret.a[0]=a[0]+b.a[0]; for(int i=1,k=0;i<=a[0];++i,++k) for(int j=1;j<=b.a[0];++j){ ret.a[j+k]+=a[i]*b.a[j]; ret.a[j+k+1]+=ret.a[j+k]/base; ret.a[j+k]%=base; } if(ret.a[ret.a[0]+1]) ret.a[0]++; while(ret.a[0]>0&&!ret.a[ret.a[0]]) ret.a[0]--; return ret; } node operator / (const node &b)const{ node ret,k; ret.cl();k.cl(); for(int i=a[0];i>=1;--i){ k.pushback(a[i]); k.rev(); while(!(k<b)) k=k-b,ret.a[i]++; k.rev(); } ret.a[0]=a[0]; while(ret.a[0]>0&&!ret.a[ret.a[0]]) ret.a[0]--; return ret; } void print(int fx){ printf("%d",fx*a[a[0]]); for(int i=a[0]-1;i>=1;--i) printf("%0*d",p,a[i]); puts(""); }}x[3];void add(node u,node v){ node ans=u+v; ans.print(1); }void sub(node u,node v){ node ans; if(v<u) ans=u-v,ans.print(1); else ans=v-u,ans.print(-1); }void mul(node u,node v){ node ans=u*v; ans.print(1); }void div(node u,node v){ node ans; if(v<u) ans=u/v; else ans.cl(); ans.print(1);}int main(){ scanf("%s%s",x[1].s+1,x[2].s+1); x[1].update(x[1].s+1); x[2].update(x[2].s+1); add(x[1],x[2]); sub(x[1],x[2]); mul(x[1],x[2]); div(x[1],x[2]); return 0;}/*19987787889 98776665*ans=1974327028402810185*//*19987787889 98776665/ans=202*/
阅读全文
0 0
- 高精度加减乘除
- 高精度加减乘除
- 高精度加减乘除
- 高精度 <加减乘除>
- 高精度加减乘除
- 大数据/高精度加减乘除
- 高精度abcdefg加减乘除模板。
- 高精度加减乘除模板
- 【多题合集】高精度加减乘除
- 高精度 加减乘除 【精华】
- 大数高精度加减乘除模板
- 大整数高精度加减乘除
- 高精度算法,加减乘除
- 大数(高精度)加减乘除取模
- 高精度加减乘除类的实现
- 高精度---BigDecimal类的加减乘除
- 高精度运算 加减乘除阶乘幂
- 高精度加减乘除的笔记+代码
- 训练日记-35
- vue实现横向滚动效果(better-scroll)
- BMP文件格式详解(BMP file format)
- 取消CSDN的手机绑定
- 数据结构栈和队列
- 高精度 <加减乘除>
- 68. Text Justification
- Mac && XCode 操作入门
- struts2重点、精华、用法总结(七):数据处理机制之struts标签
- android o 适配小记
- 分治算法 快速排序
- 说说激活函数
- 漫步最优化三十六——基本共轭方向法
- http创建环境