XDOJ 1046 - 高精度模板综合测试 - [高精度模板]
题目链接:http://acm.xidian.edu.cn/problem.php?id=1046
题目描述
请输出两个数的和,差,积,商,取余。注意不要有前导零。
输入
多组数据,每组数据是两个整数A,B(0<=A<=10^100,0<B<=10^100).
输出
对于每组数据,输出五个整数,分别代表A+B,A-B,A*B,A/B,A%B.
样例输入
5 2
11 3
样例输出
7 3 10 2 1
14 8 33 3 2
模板注释:
本模板只能处理十进制非负大整数。
AC代码:
#include<bits/stdc++.h>
using namespace std; struct BigInt
{
int len,d[]; void clean(){while(len> && !d[len-]) len--;}
string str()const
{
string s;
for(int i=;i<len;i++) s+=d[len--i]+'';
return s;
} BigInt(){memset(d,,sizeof(d));len=;}
BigInt(int num){*this=num;}
BigInt(char* num){*this=num;} bool operator<(const BigInt& oth)const
{
if(len!=oth.len) return len<oth.len;
for(int i=len-;i>=;i--) if(d[i]!=oth.d[i]) return d[i]<oth.d[i];
return false;
}
bool operator>(const BigInt& oth)const{return oth<*this;}
bool operator<=(const BigInt& oth)const{return !(oth<*this);}
bool operator>=(const BigInt& oth)const{return !(*this<oth);}
bool operator!=(const BigInt& oth)const{return oth<*this || *this<oth;}
bool operator==(const BigInt& oth)const{return !(oth<*this) && !(*this<oth);} BigInt operator=(const char* num)
{
memset(d,,sizeof(d));
len=strlen(num);
for(int i=;i<len;i++) d[i]=num[len--i]-'';
clean();
return *this;
}
BigInt operator=(int num)
{
char s[];
sprintf(s,"%d",num);
return *this=s;
}
BigInt operator+(const BigInt& oth)const
{
BigInt c;
c.len=max(len,oth.len);
for(int i=;i<=c.len;i++) c.d[i]=;
for(int i=;i<c.len;i++)
{
c.d[i]+=(i<len?d[i]:)+(i<oth.len?oth.d[i]:);
c.d[i+]+=c.d[i]/;
c.d[i]%=;
}
c.len+=(c.d[c.len]>);
c.clean();
return c;
}
BigInt operator-(const BigInt& oth)const
{
BigInt c=*this;
if(c<oth) printf("Produce negative number!\n");
int i;
for(i=;i<oth.len;i++)
{
c.d[i]-=oth.d[i];
if(c.d[i]<) c.d[i]+=, c.d[i+]--;
}
while(c.d[i]<) c.d[i++]+=, c.d[i]--;
c.clean();
return c;
}
BigInt operator*(const BigInt& oth)const
{
BigInt c;
for(int i=;i<len;i++) for(int j=;j<oth.len;j++) c.d[i+j]+=d[i]*oth.d[j];
for(int i=;i<len+oth.len || !c.d[i];c.len=++i) c.d[i+]+=c.d[i]/, c.d[i]%=;
c.clean();
return c;
}
BigInt operator/(const BigInt& oth)const
{
BigInt c=*this, r=;
for(int i=;i<len;i++)
{
r=r*+c.d[len--i];
int j;
for(j=;j<;j++) if(r<oth*(j+)) break;
c.d[len--i]=j;
r=r-oth*j;
}
c.clean();
return c;
}
BigInt operator%(const BigInt& oth)
{
BigInt r=;
for(int i=;i<len;i++)
{
r=r*+d[len--i];
int j;
for(j=;j<;j++) if(r<oth*(j+)) break;
r=r-oth*j;
}
return r;
}
BigInt operator+=(const BigInt& oth)
{
*this=*this+oth;
return *this;
}
BigInt operator*=(const BigInt& oth)
{
*this=*this*oth;
return *this;
}
BigInt operator-=(const BigInt& oth)
{
*this=*this-oth;
return *this;
}
BigInt operator/=(const BigInt& oth)
{
*this=*this/oth;
return *this;
}
};
istream& operator>>(istream& in, BigInt& x)
{
string s;
in>>s;
x=s.c_str();
return in;
}
ostream& operator<<(ostream& out,const BigInt& x)
{
out<<x.str();
return out;
} int main()
{
BigInt a,b;
while(cin>>a>>b)
{
cout<<a+b<<" ";
if(a<b) cout<<'-'<<b-a<<" ";
else cout<<a-b<<" ";
cout<<a*b<<" ";
cout<<a/b<<" ";
cout<<a%b<<endl;
}
}
XDOJ 1046 - 高精度模板综合测试 - [高精度模板]的更多相关文章
- Django(4)html模板继承、模板导入、分页实现
1.获取所有请求信息 导入模块:from django.core.handlers.wsgi import WSGIRequest request.environ:包含所有的请求信息,可以打印看一下, ...
- [Reprint] C++函数模板与类模板实例解析
这篇文章主要介绍了C++函数模板与类模板,需要的朋友可以参考下 本文针对C++函数模板与类模板进行了较为详尽的实例解析,有助于帮助读者加深对C++函数模板与类模板的理解.具体内容如下: 泛型编程( ...
- C++ Primer 学习笔记_76_模板与泛型编程 --模板定义[续]
模板与泛型编程 --模板定义[续] 四.模板类型形參 类型形參由keywordclass或 typename后接说明符构成.在模板形參表中,这两个keyword具有同样的含义,都指出后面所接的名字表示 ...
- 织梦CMS(dedecms)栏目属性及系统封面模板、列表模板、文章模板区别和路径设置解答
问题一:(织梦"栏目管理"的"常规选项"中3个栏目属性分析?) 织梦CMS的栏目属性分成三种, -->最终列表栏目 -->频道封面 -->外部 ...
- (转)DEDECMS模板原理、模板标签学习 - .Little Hann
本文,小瀚想和大家一起来学习一下DEDECMS中目前所使用的模板技术的原理: 什么是编译式模板.解释式模板,它们的区别是什么? 模板标签有哪些种类,它们的区别是什么,都应用在哪些场景? 学习模板的机制 ...
- C++ Primer 学习笔记_84_模板与泛型编程 --模板特化
模板与泛型编程 --模板特化 引言: 我们并不总是能够写出对全部可能被实例化的类型都最合适的模板.某些情况下,通用模板定义对于某个类型可能是全然错误的,通用模板定义或许不能编译或者做错误的事情;另外一 ...
- Django模板-分离的模板
上一篇Django模板-在视图中使用模板最后的问题,我们需要把数据和展现分离开. 你可能首先考虑把模板保存在文件系统的某个位置并用 Python 内建的文件操作函数来读取文件内容. 假设文件保存在 E ...
- C++ Primer 学习笔记_85_模板与泛型编程 --模板特化[续]
模板与泛型编程 --模板特化[续] 三.特化成员而不特化类 除了特化整个模板之外,还能够仅仅特化push和pop成员.我们将特化push成员以复制字符数组,而且特化pop成员以释放该副本使用的内存: ...
- C++ Primer 学习笔记_75_模板与泛型编程 --模板定义
模板与泛型编程 --模板定义 引言: 所谓泛型程序就是以独立于不论什么特定类型的方式编写代码.使用泛型程序时,我们须要提供详细程序实例所操作的类型或值. 模板是泛型编程的基础.使用模板时能够无须了解模 ...
随机推荐
- 整死你个妖精,CDN西游捉妖记!
CDN的降价潮和撕逼季已过,终于轮到小黑羊来做个科普啦. 这事儿,要从西游记取经开始…… [本图来自肖传湛个人网站:www.moko.cc/hiyoko] 1300年前,唐僧师徒取经要跋涉十万八千里, ...
- Eclipse Unhandled event loop exception GC overhead limit exceeded
修改Eclipse的配置文件:
- 永久关闭selinux
selinux这东西,有时候真让人搞不懂. 临时关闭: setenforce 0 getenforce #查看状态是否是disabled 永久关闭: vim /etc/sysconfig/selinu ...
- arcgismanager登陆信息不对
arcgis版本:arcgis10 安装arcgis server后(java版的win764位系统),发现arcgis管理器登陆界面(http://localhost:8099/arcgismana ...
- SNF开发平台WinForm-审核流使用方法样例
一.效果如下: 二.如何实现 1.程序的数据表设计规范,参考<09.SNF-C#编程规范V1.5.docx>文件. 2.程序操作程序 2.1.在程序页面拖拽控件 2.2.程序的Load事件 ...
- Xilinx 常用模块汇总(verilog)【04】
作者:桂. 时间:2018-05-15 13:07:02 链接:http://www.cnblogs.com/xingshansi/p/9040472.html 前言 Xilinx 常用模块汇总(v ...
- ajax的工作原理2
Ajax是异步javascript和xml,可以在不重新加载整个网页的情况下,与服务器异步数据交换,对网页中某个部分进行局部刷新. Ajax是如何实现局部刷新的: 在脚本设置window对象的loca ...
- 假设分配给命令的连接位于本地挂起事务中,ExecuteReader 要求命令拥有事务。命令的 Transaction 属性尚未初始化
{System.InvalidOperationException: 假设分配给命令的连接位于本地挂起事务中.ExecuteReader 要求命令拥有事务.命令的 Transaction 属性尚未初始 ...
- WebApi XML,Json格式自定义,IEnumerable<T>,ArrayOf
global.ascx中application-start() GlobalConfiguration.Configuration.Formatters.XmlFormatter.UseXmlSeri ...
- git配置用户名邮箱,全局配置/单仓库配置
在项目根目录下进行单仓库配置(作用域只在本仓库下): git config user.name "gitlab's Name" git config user.email &quo ...