存个代码

struct BigInterger {
static const int BASE = 1e8;
static const int WIDTH = ;
vector<int> s;
BigInterger(long long num = ) { *this = num; }
BigInterger operator =(long long num) {
s.clear();
do {
s.push_back(num%BASE);
num /= BASE;
} while (num > );//num==0
return *this;
}
BigInterger operator =(const string& str) {
s.clear();
int x, len = (str.length() - ) / WIDTH + ;//len==width
for (int i = ; i < len; i++) {
int end = str.length() - i*WIDTH;
int start = max(, end - WIDTH);
sscanf(str.substr(start, end - start).c_str(), "%d", &x);
s.push_back(x);
}
return *this;
} BigInterger operator +(const BigInterger& b)const {
BigInterger c;
c.s.clear();
for (int i = , g = ;; i++) {
if (g == && i >= max(s.size(), b.s.size()))break;//此处bug
int x = g;
if (i < s.size())x += s[i];
if (i < b.s.size()) x += b.s[i];
c.s.push_back(x%BASE);
g = x / BASE;
}
return c;
}
BigInterger operator +=(const BigInterger& b) {
*this = *this + b; return *this;
} bool operator<(const BigInterger& b)const {
if (s.size() != b.s.size()) return s.size() < b.s.size();
for (int i = s.size() - ; i >= ; i--) {
if (s[i] != b.s[i]) return s[i] < b.s[i];//Width 个数字一起比
}
return false;//==
}
bool operator>(const BigInterger &b)const { return b < *this; }
bool operator<=(const BigInterger &b)const { return !(b < *this); }
bool operator>=(const BigInterger &b)const { return !(*this < b); }
bool operator!=(const BigInterger &b)const { return b < *this || *this<b; }
bool operator==(const BigInterger &b)const { return!(b < *this) && !(*this<b); }
};
ostream& operator <<(ostream &out, const BigInterger& x) {
out << x.s.back();
for (int i = x.s.size() - ; i >= ; i--) {
char buf[];
sprintf(buf, "%08d", x.s[i]);
for (int j = ; j < strlen(buf); j++)cout << buf[j];
}
return out;
}
istream&operator>>(istream &in, BigInterger&x) {
string s;
if (!(in >> s))return in;
x = s;
return in;
}
 

【紫书】BigInteger 高精度类型 原书上有一个bug:A+B!=B+A的更多相关文章

  1. sqlite在Android上的一个bug:SQLiteCantOpenDatabaseException when nativeExecuteForCursorWindow

    更多内容在这里查看 https://ahangchen.gitbooks.io/windy-afternoon/content/ ::-/com.company.product W/System.er ...

  2. 关于mysql设置外键,实现参照性完整性约束,以及workbench上的一个bug(?)

    一.本次数据库中有student,course,sc表,其设置情况 -- 创建course表 CREATE TABLE `course` ( `cno` ) NOT NULL, `cname` ) D ...

  3. iOS UIPrintInteractionController在iPad的 iOS10 和 11上的奇怪bug

    今天在弹出UIPrintInteractionController的时候,在ios10 和11的ipad 上测试,发现一直是protrait 方向弹出,结果就出现如下图的bug: 研究了好长时间,发现 ...

  4. 点石成金:访客至上的网页设计秘笈(原书第2版) 中文PDF版

    可用性设计是Web设计中最重要也是难度最大的一项任务.本书作者根据多年从业的经验,剖析用户的心理,在用户使用的模式.为扫描进行设计.导航设计.主页布局.可用性测试等方面提出了许多独特的观点,并给出了大 ...

  5. 学习PHP爬虫--《Webbots、Spiders和Screen Scrapers:技术解析与应用实践(原书第2版)》

    <Webbots.Spiders和Screen Scrapers:技术解析与应用实践(原书第2版)> 译者序 前言 第一部分 基础概念和技术 第1章 本书主要内容3 1.1 发现互联网的真 ...

  6. PHP和MySQL Web开发(原书第4版) 高清PDF+源代码

    PHP和MySQL Web开发(原书第4版) 高清PDF+源代码 [日期:2014-08-06] 来源:Linux社区  作者:Linux [字体:大 中 小]     内容简介 <PHP和My ...

  7. ROS机器人程序设计(原书第2版)补充资料 (捌) 第八章 导航功能包集入门 navigation

    ROS机器人程序设计(原书第2版)补充资料 (捌) 第八章 导航功能包集入门 navigation 书中,大部分出现hydro的地方,直接替换为indigo或jade或kinetic,即可在对应版本中 ...

  8. ROS机器人程序设计(原书第2版)补充资料 (柒) 第七章 3D建模与仿真 urdf Gazebo V-Rep Webots Morse

    ROS机器人程序设计(原书第2版)补充资料 (柒) 第七章 3D建模与仿真 urdf Gazebo V-Rep Webots Morse 书中,大部分出现hydro的地方,直接替换为indigo或ja ...

  9. ROS机器人程序设计(原书第2版)补充资料 (陆) 第六章 点云 PCL

    ROS机器人程序设计(原书第2版)补充资料 (陆) 第六章 点云 PCL 书中,大部分出现hydro的地方,直接替换为indigo或jade或kinetic,即可在对应版本中使用. RGBD深度摄像头 ...

随机推荐

  1. Java查找替换文本文件内容

    文本替换几乎是所有文本编辑器都支持的功能,但是要限制在编辑其中才可以执行该功能.本实例实现了制定文本文件的内容替换,并且不需要再编辑其中打开文本文件. 思路: 先看视图层,要有一个JButton控件用 ...

  2. 5 -- Hibernate的基本用法 --3 Hibernate的体系结构

    ⊙ SessionFactory : 这是Hibernate的关键对象,它是单个数据库映射关系经过编译后的内存镜像,也是线程安全的.它是生成Session的工厂,本身需要依赖于ConnectionPr ...

  3. bat批处理设置静态、动态、ping、查看网络配置

    @echo off :startIP set /p source=S or D or C or P or E: echo source:%source% if /i "%source%&qu ...

  4. 8 -- 深入使用Spring -- 7... Spring 整合 Struts 2

    8.7 Spring 整合 Struts2 8.7.1 启动Spring 容器 8.7.2 MVC框架与Spring整合的思考 8.7.3 让Spring管理控制器 8.7.4 使用自动装配

  5. centos 上不了网了

    昨天还用的好好的,今天就上不了网了,郁闷,不过,正好是一次学习linux网络配置的好机会,这会已经把它折腾好了,此文就是在linux下面的浏览器中写的! 先检查一下虚拟机中的网络设置是否正常,由于我的 ...

  6. NTP服务器时间集群借节点之间同步

    1.三个节点时间同步,cdh1,cdh2,cdh3 2.做法:cdh1从网络时间同步,然后cdh2和cdh3从cdh1节点同步 3.安装与自启动设置 yum install ntp 按上面的安装方式在 ...

  7. 使用IDEA实现tomcat的热加载

    1.打开tomcat的edit configuration,一定要选择war exploded  2.选择update classes and resources  3.配置基本就是这样,后面选择de ...

  8. 禁用Visual Studio 2013的Browser Link功能 -调试不断请求http://localhost:6154/c4ad1c693ebf428283832eaa827f9c6e/arterySignalR/poll?transport=longPolling...

    关于禁用查到的解决: 作者:donny945 https://my.oschina.net/ind/blog/359003 今天浏览器调试代码的时候,一直出现以下的请求,导致需要看的请求都被淹没了,之 ...

  9. linux禁止IPv6

    1. 禁止加载IPv6模块 # echo "install ipv6 /bin/true" > /etc/modprobe.d/disable-ipv6.conf 每当系统需 ...

  10. Material Design系列第三篇——Using the Material Theme

    Using the Material Theme This lesson teaches you to Customize the Color Palette Customize the Status ...