xdoj 1046: 高精度模板综合测试
来源:互联网 发布:telnet端口 登录 内网 编辑:程序博客网 时间:2024/06/07 20:30
题目描述
请输出两个数的和,差,积,商,取余。注意不要有前导零。
输入
多组数据,每组数据是两个整数A,B(0<=A<=10^100,0<B<=10^100).
输出
对于每组数据,输出五个整数,分别代表A+B,A-B,A*B,A/B,A%B.
样例输入
5 211 3
样例输出
7 3 10 2 114 8 33 3 2
#include <iostream>#include <cstring>#include <algorithm>#include <string>#include <cmath>using namespace std;typedef unsigned long ul;string v;int absInt(int a){return a>0?a:a*-1;}//if a>=b return true;bool compare(string a,string b){ if(a[0] == '-') return false; if(a.size()>b.size()) return true; else if(a.size()==b.size()) return a>=b; return false;}string add(string a,string b){ reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); if(a.size()<b.size()) swap(a, b); bool f = 0; for(int i=0;i<b.size();i++) { a[i] = a[i] +b[i]-48+f; if(a[i]>'9') { f = true; a[i] = a[i] -10; } else f = false; } for(ul i=b.size();i<a.size();i++) { a[i] = a[i]+f; if(a[i]>'9') { f = true; a[i] = a[i] -10; } else f = false; } if(f) a = a+"1"; reverse(a.begin(),a.end()); return a; }string sub(string a,string b){ bool flag = compare(a, b); if(!flag) swap(a,b); reverse(a.begin(), a.end()); reverse(b.begin(), b.end()); bool f = false; for(int i=0;i<b.size();i++) { a[i] = a[i] + '0' - b[i] -f; if(a[i]<'0') { f = true; a[i] = a[i] + 10; } else f = false; } for(ul i=b.size();i<a.size();i++) { a[i] = a[i] - f; if(a[i]<'0') { f = true; a[i] = a[i] + 10; } else f = false; } reverse(a.begin(),a.end()); while(*a.begin()=='0') a.erase(a.begin()); if(a.size()==0) a = "0"; if(!flag) a = "-"+a; return a; }string mul(string a,int b){ int flag = 0; for(int i=a.size()-1;i>=0;i--) { int str = (a[i]-48)*b + flag; a[i] = str%10 +48; flag = str/10; } if(flag) a.insert(a.begin(), flag+48); while(*a.begin()=='0') a.erase(a.begin()); if(a.size()==0) a = "0"; return a;}string mul(string a,string b){ string c; if(a.size()<b.size()) swap(a, b); c = mul(a,b[b.size()-1]-'0'); string k = "0"; for(int i=b.size()-2;i>=0;i--) { string f = mul(a,b[i]-'0'); f = f+k; c = add(c, f); k = k + "0"; } return c;}string div(string a,string b){ string c; if(a.size()<b.size()) { c = "0"; v = a; } if(a.size()==b.size()&&a<b) { c = "0"; v = a; } string f(a.begin(),a.begin()+b.size()); int res = 0; while(compare(f, b)) { f = sub(f,b); res++; } c.insert(c.end(), res+'0'); for(ul i=b.size();i<a.size();i++) { f.insert(f.end(), a[i]); while(*f.begin()=='0') f.erase(f.begin()); int res = 0; while(compare(f, b)) { f = sub(f,b); res++; } c.insert(c.end(), res+'0'); } v = f; while(*c.begin()=='0') c.erase(c.begin()); if(c.size()==0) c = "0"; return c;}int main(){ string a,b; while(cin>>a>>b) { cout<<add(a,b)<<' '<<sub(a,b)<<' '<<mul(a,b)<<' '<<div(a, b)<<' '<<v<<endl; } return 0;}/*input:5 2output:7 3 10 2 1input:11 3output:14 8 33 3 2*/
//之后会扩展到整数域的计算
1 0
- xdoj 1046: 高精度模板综合测试
- 综合测试
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 高精度模板
- 【模板】高精度
- Java中存储金额用什么数据类型?
- NestedScrollingParent, NestedScrollingChild 详解
- Linux MASM汇编
- jquery post时content-type的几种取值
- Elasticsearch是什么以及核心概念
- xdoj 1046: 高精度模板综合测试
- PTA-数据结构-自测-1
- 解决微信小程序ios请求ssl错误问题 windows server 2008 R2默认ssl2.0 改成1.2的
- Servlet3.0下@WebFilter注解配置Filter
- 在List查找指定名称的成员的两种写法和搜索所有指定类物体的写法
- 输入一行字符,分别统计出其中英文字母、空格、数字和其他字符的个数
- 数据库
- ubuntu下caffe实战---mnist数据集测试
- 初学编程-----数组运算