高精——模板

紫书:

#include <iostream>  
#include <string>  
#include <cstring>  
#include <cstdio>  
using namespace std;  const int maxn = 1000;  struct bign{  int d[maxn], len;  void clean() { while(len > 1 && !d[len-1]) len--; }  bign()          { memset(d, 0, sizeof(d)); len = 1; }  bign(int num)   { *this = num; }   bign(char* num) { *this = num; }  bign operator = (const char* num){  memset(d, 0, sizeof(d)); len = strlen(num);  for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0';  clean();  return *this;  }  bign operator = (int num){  char s[20]; sprintf(s, "%d", num);  *this = s;  return *this;  }  bign operator + (const bign& b){  bign c = *this; int i;  for (i = 0; i < b.len; i++){  c.d[i] += b.d[i];  if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++;  }  while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++;  c.len = max(len, b.len);  if (c.d[i] && c.len <= i) c.len = i+1;  return c;  }  bign operator - (const bign& b){  bign c = *this; int i;  for (i = 0; i < b.len; i++){  c.d[i] -= b.d[i];  if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;  }  while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--;  c.clean();  return c;  }  bign operator * (const bign& b)const{  int i, j; bign c; c.len = len + b.len;   for(j = 0; j < b.len; j++) for(i = 0; i < len; i++)   c.d[i+j] += d[i] * b.d[j];  for(i = 0; i < c.len-1; i++)  c.d[i+1] += c.d[i]/10, c.d[i] %= 10;  c.clean();  return c;  }  bign operator / (const bign& b){  int i, j;  bign c = *this, a = 0;  for (i = len - 1; i >= 0; i--)  {  a = a*10 + d[i];  for (j = 0; j < 10; j++) if (a < b*(j+1)) break;  c.d[i] = j;  a = a - b*j;  }  c.clean();  return c;  }  bign operator % (const bign& b){  int i, j;  bign a = 0;  for (i = len - 1; i >= 0; i--)  {  a = a*10 + d[i];  for (j = 0; j < 10; j++) if (a < b*(j+1)) break;  a = a - b*j;  }  return a;  }  bign operator += (const bign& b){  *this = *this + b;  return *this;  }  bool operator <(const bign& b) const{  if(len != b.len) return len < b.len;  for(int i = len-1; i >= 0; i--)  if(d[i] != b.d[i]) return d[i] < b.d[i];  return false;  }  bool operator >(const bign& b) const{return b < *this;}  bool operator<=(const bign& b) const{return !(b < *this);}  bool operator>=(const bign& b) const{return !(*this < b);}  bool operator!=(const bign& b) const{return b < *this || *this < b;}  bool operator==(const bign& b) const{return !(b < *this) && !(b > *this);}  string str() const{  char s[maxn]={};  for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0';  return s;  }  
};  istream& operator >> (istream& in, bign& x)  
{  string s;  in >> s;  x = s.c_str();  return in;  
}  ostream& operator << (ostream& out, const bign& x)  
{  out << x.str();  return out;  
}  

最全:

#include<string>
#include<iostream>
#include<iosfwd>
#include<cmath>
#include<cstring>
#include<stdlib.h>
#include<stdio.h>
#include<cstring>
using namespace std;const int  MAX_L=2005; //最大长度,可以修改class bign
{
public:int len, s[MAX_L];//数的长度,记录数组
//构造函数bign();bign(const char*);bign(int);bool sign;//符号 1正数 0负数string toStr() const;//转化为字符串,主要是便于输出friend istream& operator>>(istream &,bign &);//重载输入流friend ostream& operator<<(ostream &,bign &);//重载输出流
//重载复制bign operator=(const char*);bign operator=(int);bign operator=(const string);
//重载各种比较bool operator>(const bign &) const;bool operator>=(const bign &) const;bool operator<(const bign &) const;bool operator<=(const bign &) const;bool operator==(const bign &) const;bool operator!=(const bign &) const;
//重载四则运算bign operator+(const bign &) const;bign operator++();bign operator++(int);bign operator+=(const bign&);bign operator-(const bign &) const;bign operator--();bign operator--(int);bign operator-=(const bign&);bign operator*(const bign &)const;bign operator*(const int num)const;bign operator*=(const bign&);bign operator/(const bign&)const;bign operator/=(const bign&);
//四则运算的衍生运算bign operator%(const bign&)const;//取模(余数)bign factorial()const;//阶乘bign Sqrt()const;//整数开根(向下取整)bign pow(const bign&)const;//次方
//一些乱乱的函数void clean();
};
#define max(a,b) a>b ? a : b
#define min(a,b) a<b ? a : bbign::bign()
{memset(s, 0, sizeof(s));len = 1;sign = 1;
}bign::bign(const char *num)
{*this = num;
}bign::bign(int num)
{*this = num;
}string bign::toStr() const
{string res;res = "";for (int i = 0; i < len; i++)res = (char)(s[i] + '0') + res;if (res == "")res = "0";if (!sign&&res != "0")res = "-" + res;return res;
}istream &operator>>(istream &in, bign &num)
{string str;in>>str;num=str;return in;
}ostream &operator<<(ostream &out, bign &num)
{out<<num.toStr();return out;
}bign bign::operator=(const char *num)
{memset(s, 0, sizeof(s));char a[MAX_L] = "";if (num[0] != '-')strcpy(a, num);elsefor (int i = 1; i < strlen(num); i++)a[i - 1] = num[i];sign = !(num[0] == '-');len = strlen(a);for (int i = 0; i < strlen(a); i++)s[i] = a[len - i - 1] - 48;return *this;
}bign bign::operator=(int num)
{char temp[MAX_L];sprintf(temp, "%d", num);*this = temp;return *this;
}bign bign::operator=(const string num)
{const char *tmp;tmp = num.c_str();*this = tmp;return *this;
}bool bign::operator<(const bign &num) const
{if (sign^num.sign)return num.sign;if (len != num.len)return len < num.len;for (int i = len - 1; i >= 0; i--)if (s[i] != num.s[i])return sign ? (s[i] < num.s[i]) : (!(s[i] < num.s[i]));return !sign;
}bool bign::operator>(const bign&num)const
{return num < *this;
}bool bign::operator<=(const bign&num)const
{return !(*this>num);
}bool bign::operator>=(const bign&num)const
{return !(*this<num);
}bool bign::operator!=(const bign&num)const
{return *this > num || *this < num;
}bool bign::operator==(const bign&num)const
{return !(num != *this);
}bign bign::operator+(const bign &num) const
{if (sign^num.sign){bign tmp = sign ? num : *this;tmp.sign = 1;return sign ? *this - tmp : num - tmp;}bign result;result.len = 0;int temp = 0;for (int i = 0; temp || i < (max(len, num.len)); i++){int t = s[i] + num.s[i] + temp;result.s[result.len++] = t % 10;temp = t / 10;}result.sign = sign;return result;
}bign bign::operator++()
{*this = *this + 1;return *this;
}bign bign::operator++(int)
{bign old = *this;++(*this);return old;
}bign bign::operator+=(const bign &num)
{*this = *this + num;return *this;
}bign bign::operator-(const bign &num) const
{bign b=num,a=*this;if (!num.sign && !sign){b.sign=1;a.sign=1;return b-a;}if (!b.sign){b.sign=1;return a+b;}if (!a.sign){a.sign=1;b=bign(0)-(a+b);return b;}if (a<b){bign c=(b-a);c.sign=false;return c;}bign result;result.len = 0;for (int i = 0, g = 0; i < a.len; i++){int x = a.s[i] - g;if (i < b.len) x -= b.s[i];if (x >= 0) g = 0;else{g = 1;x += 10;}result.s[result.len++] = x;}result.clean();return result;
}bign bign::operator * (const bign &num)const
{bign result;result.len = len + num.len;for (int i = 0; i < len; i++)for (int j = 0; j < num.len; j++)result.s[i + j] += s[i] * num.s[j];for (int i = 0; i < result.len; i++){result.s[i + 1] += result.s[i] / 10;result.s[i] %= 10;}result.clean();result.sign = !(sign^num.sign);return result;
}bign bign::operator*(const int num)const
{bign x = num;bign z = *this;return x*z;
}
bign bign::operator*=(const bign&num)
{*this = *this * num;return *this;
}bign bign::operator /(const bign&num)const
{bign ans;ans.len = len - num.len + 1;if (ans.len < 0){ans.len = 1;return ans;}bign divisor = *this, divid = num;divisor.sign = divid.sign = 1;int k = ans.len - 1;int j = len - 1;while (k >= 0){while (divisor.s[j] == 0) j--;if (k > j) k = j;char z[MAX_L];memset(z, 0, sizeof(z));for (int i = j; i >= k; i--)z[j - i] = divisor.s[i] + '0';bign dividend = z;if (dividend < divid) { k--; continue; }int key = 0;while (divid*key <= dividend) key++;key--;ans.s[k] = key;bign temp = divid*key;for (int i = 0; i < k; i++)temp = temp * 10;divisor = divisor - temp;k--;}ans.clean();ans.sign = !(sign^num.sign);return ans;
}bign bign::operator/=(const bign&num)
{*this = *this / num;return *this;
}bign bign::operator%(const bign& num)const
{bign a = *this, b = num;a.sign = b.sign = 1;bign result, temp = a / b*b;result = a - temp;result.sign = sign;return result;
}bign bign::pow(const bign& num)const
{bign result = 1;for (bign i = 0; i < num; i++)result = result*(*this);return result;
}bign bign::factorial()const
{bign result = 1;for (bign i = 1; i <= *this; i++)result *= i;return result;
}void bign::clean()
{if (len == 0) len++;while (len > 1 && s[len - 1] == '\0')len--;
}bign bign::Sqrt()const
{if(*this<0)return -1;if(*this<=1)return *this;bign l=0,r=*this,mid;while(r-l>1){mid=(l+r)/2;if(mid*mid>*this)r=mid;elsel=mid;}return l;
}bign num0,num1,res;int main() {num0 = 1, num1 = 2;res=num0-num1;cout << res << endl;return 0;
}

出处:
紫书和
https://www.cnblogs.com/HarryGuo2012/p/4524041.html

转载于:https://www.cnblogs.com/Menteur-Hxy/p/9248016.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/250937.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

认识及实现MVC

gitee M&#xff1a;Model 数据模型&#xff08;模型层&#xff09;→ 操作数据库&#xff08;对数据进行增删改查&#xff09; V&#xff1a;View视图层 → 显示视图或视图模板 C&#xff1a;Controller 控制器层 → 逻辑层 数据和视图关联挂载和基本的逻辑操作 API层 前端请…

算法 --- 翻转二叉树

解(C): 1.二叉树判空 if(root 0) 或 if(root nullptr); 2.二叉树的左子树: root->left . 3.使用递归,将当前根节点的左右指针指向互换左向右子树(此时右子树也进行了翻转) // C /*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode…

float 常见用法与问题--摘抄

float 属性绝对是众多切图仔用的最多的 CSS 属性之一&#xff0c;它的用法很简单&#xff0c;常用值就 left、right、none 三个&#xff0c;但是它的特性你真的弄懂了吗&#xff1f; 我会在这里介绍我对 float 的认识与使用&#xff0c;以及使用过程中遇到的问题。 对 float 的…

javascipt -- find方法和findIndex方法的实现

find: 根据传入的条件函数,返回符合条件的第一项 var arr [{id: 1, name: zs, age: 18},{id: 2, name: zs, age: 17},{id: 3, name: ls, age: 16},{id: 4, name: ls, age: 15}]Array.prototype._find_ function(cb){for(var i0; i< this.length; i){if(cb(this[i],i)){ret…

bzoj 2179 FFT快速傅立叶 FFT

题面 题目传送门 解法 题如其名…… 不妨将多项式的\(x^i\)变成\(10^i\)&#xff0c;然后就是一个比较简单的FFT了 md读进来的是一个字符串&#xff0c;并且要倒序 最后注意进位问题 时间复杂度&#xff1a;\(O(n\ log\ n)\) 代码 #include <bits/stdc.h> #define N 1 &l…

【探讨】javascript事件机制底层实现原理

前言 又到了扯淡时间了&#xff0c;我最近在思考javascript事件机制底层的实现&#xff0c;但是暂时没有勇气去看chrome源码&#xff0c;所以今天我来猜测一把 我们今天来猜一猜&#xff0c;探讨探讨&#xff0c;javascript底层事件机制是如何实现的 博客里面关于事件绑定与执行…

node --- 在node中使用mongoosemongoDB的安装

*首先确保,你的电脑安装了mongodb,网址: mongodb官网 *使用npm安装 mongoose: mongoose官网 ps:mongoose是Node中操作mongoDB的第三方插件.用于提高数据库操作效率(相当于在mongoDB上封装了一次,暴露出更友好的API) MongoDB的安装 1.下载地址 2.下载好了后,傻瓜式的安装(我的…

websocket demo

git node.js创建websocket 的服务 Nodejs Websocket包 ws.createServer([options], [callback]) The callback is a function which is automatically added to the “connection” event. 前端代码 1. 创建实例、打开连接 this.websocket new WebSocket(ws://127.0.0.1:80…

shell常用命令总结总结

打rpm包&#xff1a; rpmbuild -bb SPECS/smplayer.spec --define "_topdir pwd" 安装rpm包&#xff1a; rpm -ivh [rpm包文件] 如果安装不上 rpm -ivh [rpm包文件] --force #强制安装 打包的时候可能需要一些依赖&#xff1a; dnf install 【依赖文件名】 sed -i常用…

Filter

一、简介 Filter也称之为过滤器&#xff0c;它是Servlet技术中最激动人心的技术&#xff0c;WEB开发人员通过Filter技术&#xff0c;对web服务器管理的所有web资源&#xff1a;例如Jsp&#xff0c;Servlet&#xff0c;静态图片文件或静态html文件进行拦截&#xff0c;从而实现一…

前端面试手写题

深拷贝 // 深拷贝 function deepClone(ori) {let tar;if (typeof ori object && ori ! null) {tar Array.isArray(ori) ? [] : {}for (let k in ori) {if (ori.hasOwnProperty(k)) {tar[k] deepClone(ori[k])}}} else {tar ori}return tar}继承 // 圣杯模式实现…

node --- 使用express.Router与body-parser

express框架提供了一个Router方法,用于监听路由 // 命令行(windows*64) npm install express --save// router.js const express require("express"); // 定义路由 const router express.Router();// 处理http://host:port/students/ 路由(GET方法) router.get…

python基础1 第一天

TEST 1 阿斯蒂芬 day1test1 while 1&#xff1a;print&#xff08;333&#xff09; import randomprint转载于:https://www.cnblogs.com/shuangzhu/p/9243853.html

【数据库】《SQL必知必会 4th》部分笔记

9.汇总数据 count(*) 包括空 count(name) 不包括空 10.分组数据 group by 分组 having 过滤分组 where 过滤行 11.子查询 select .. from .. where in (select ...) 由内向外处理 A.子查询过滤 作为子查询的语句只能查询单个列。 B.作为计算字段使用子查询 select cust_name, …

微软认知服务应用秘籍 – 漫画翻译篇

概述 微软认知服务包括了影像、语音、语言、搜索、知识五大领域&#xff0c;通过对这些认知服务的独立或者组合使用&#xff0c;可以解决很多现实世界中的问题。作为AI小白&#xff0c;我们可以选择艰难地攀登崇山峻岭&#xff0c;也可以选择像牛顿一样站在巨人的肩膀上。本章节…

01 React初步认知、React元素、渲染、工程化

定义 react&#xff1a;用于构建用户界面的 JavaScript 库 &#xff08;仅负责View层渲染、应在视图上体现交互逻辑&#xff09;vue&#xff1a;渐进式JavaScript 框架&#xff08;MVVM&#xff09; 使用 引入CDN脚本添加根容器 div #app创建React组件 ReactDOM.render Re…

node --- 在express中配置使用模板引擎(art-template)

下载依赖: npm install --save art-template express-art-template配置: // app.js const express require("express"); const app express(); app.engine("html", require("express-art-template"));使用: 例如处理浏览器GET请求 /students…

PAM认证机制

一、PAM简介 Sun公司1995年开发的一种与认证相关的通用框架机制&#xff0c;PAM只关注如何为服务验证用户的API&#xff0c;通过提供一些动态链接库和一套统一的API&#xff0c;将系统提供的服务和该服务的认证方式分开&#xff1b;PAM只是一个框架而已&#xff0c;自身不做认证…

02 JSX学习

使用vite处理jsx vite引入的脚本必须是ESM的 npm init -y yarn add vite package.json 添加vite命令 index.html引入jsxJSX是什么 一种标签语法&#xff0c;在JS基础上进行的语法扩展不是字符串、也不是HTML是描述UI呈现与交互的直观的表现形式JSX被编译后会生成React元素 &am…

使用FreeCookies 控制浏览器cookies及修改http响应内容

FreeCookies 插件安装 1&#xff1a;您的计算机需要已经安装Fiddler &#xff08;如未安装&#xff0c;请至官网下载安装 http://docs.telerik.com/fiddler/configure-fiddler/tasks/configurefiddler&#xff09; 2&#xff1a;进入Fiddler安装目录下的Scripts目录下&#xff…