模板-高精度A+B,A-B
来源:互联网 发布:越狱铃声软件 编辑:程序博客网 时间:2024/06/14 07:03
题意:
求A+B,A-B;
-10^5<=A,B<=10^5;
题解:
没啥可说的。。。自用模板而已;
感觉实现注意传参要传地址否则结构体太大会RE的;
同号可以直接处理,异号加变减减变加,就转化成同号咯;
上代码;
代码:
#include<stdio.h>#include<string.h>#include<algorithm>#define N 100010#define mod 10using namespace std;char str[N];struct BIG{ bool sign; int len; int num[N]; void read() { scanf("%s",str); len=strlen(str); int i; if(str[0]=='-')sign=0,i=1; elsesign=1,i=0; for(;i<len;i++)num[len-i]=str[i]-'0'; return ; } void print() { if(!sign) printf("-"); for(int i=len;i>0;i--) printf("%d",num[i]); printf("\n"); }}a,b,In;bool operator<(BIG &a,BIG &b){ if(a.sign^b.sign) return !a.sign; if(a.len!=b.len) return (a.len>b.len)^a.sign; for(int i=a.len;i>0;i--) if(a.num[i]<b.num[i]) return a.sign; return 0;}BIG operator-(BIG &a,BIG &b);BIG operator+(BIG &a,BIG &b){ if(a.sign^b.sign) { b.sign^=1; return a-b; }BIG ret=In; int up=0,len=max(a.len,b.len),temp; for(int i=1;i<=len;i++) { temp=a.num[i]+b.num[i]+up; ret.num[i]=temp>=mod?temp-mod:temp; up=(temp>=mod); } ret.sign=a.sign; if(up) ret.num[len+1]=1,ret.len=len+1; else ret.len=len; while(ret.num[ret.len]==0&&ret.len>1) ret.len--; return ret;}BIG operator-(BIG &a,BIG &b){ if(a.sign^b.sign) { b.sign^=1; return a+b; } if(!(a<b^a.sign)) { a.sign^=1,b.sign^=1;swap(a,b); } BIG ret=In; int up=0,len=max(a.len,b.len),temp; for(int i=1;i<=len;i++) { temp=a.num[i]-b.num[i]-up; ret.num[i]=temp<0?temp+mod:temp; up=temp<0; } ret.sign=a.sign,ret.len=len; while(ret.num[ret.len]==0&&ret.len>1) ret.len--; return ret;}int main(){a.read();b.read();(a+b).print();(a-b).print();return 0;}
0 0
- 模板-高精度A+B,A-B
- USTC1046 A * B Problem 高精度乘法模板
- 1381 a*b 高精度
- 高精度大数A+B
- A+B Problem 高精度
- A + B高精度
- a+b(高精度)
- 【高精度算法】A*B
- 【高精度算法】A+B
- 高精度 A*B Problem
- 高精度 A+B Problem
- 高精度 A-B Problem
- a+b高精度
- A+B高精度问题
- 【高精度算法】A+B
- a+b 高精度
- 高精度求A+B
- SGU 112. a^b-b^a 高精度
- 汇编语言基础4
- [leetcode] Construct Binary Tree from Inorder and Postorder Traversal
- C和C++程序员面试秘籍
- MySQL按照汉字拼音字母排序
- 求一个数据结构C语言 关于关键字统计源代码
- 模板-高精度A+B,A-B
- Single Number
- SeqList.
- nefu1009消灭兔子 优先队列
- 苹果开发 笔记(38)UIImagePickerController
- WEB服务器与应用服务器
- hdu 1033 (bit masking, utilization of switch, '\0' as end of c string)
- java之弱引用
- 黑马程序员-排序(冒泡,选择,快速排序)