模板库
时间:2019-09-13
本文章向大家介绍模板库,主要包括模板库使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
基础模板
read()+print()
inline int read() { char ch; bool bj=0; while(!isdigit(ch=getchar())) bj|=(ch=='-'); int res=ch^(3<<4); while(isdigit(ch=getchar())) res=(res<<1)+(res<<3)+(ch^(3<<4)); return bj?-res:res; } void printnum(int x) { if(x>9)printnum(x/10); putchar(x%10+'0'); } inline void print(int x,char ch) { if(x<0) { putchar('-'); x=-x; } printnum(x); putchar(ch); }
fread()
struct ios{ inline char read(){ static const int IN_LEN=1<<18|1; static char buf[IN_LEN],*s,*t; return (s==t)&&(t=(s=buf)+fread(buf,1,IN_LEN,stdin)),s==t?-1:*s++; } template <typename _Tp> inline ios & operator >> (_Tp&x){ static char c11,boo; for(c11=read(),boo=0;!isdigit(c11);c11=read()){ if(c11==-1)return *this; boo|=c11=='-'; } for(x=0;isdigit(c11);c11=read())x=x*10+(c11^'0'); boo&&(x=-x); return *this; } }io;
高精度+重载运算符
const int MAX=100; struct node { int num[MAX]; node & operator = (const char*); node & operator = (int); node(); node(int); bool operator > (const node &) const; bool operator < (const node &) const; bool operator <= (const node &) const; bool operator >= (const node &) const; bool operator != (const node &) const; bool operator == (const node &) const; node operator + (const node &) const; node operator - (const node &) const; node operator * (const node &) const; node operator / (const node &) const; node operator % (const node &) const; node & operator += (const node &); node & operator -= (const node &); node & operator *= (const node &); node & operator /= (const node &); node & operator %= (const node &); }; node & node::operator = (const char* c) { memset(num,0,sizeof(num)); int n=strlen(c),j=1,k=1; for (int i=1; i<=n; i++) { if (k==10000) j++,k=1; num[j]+=k*(c[n-i]-'0'); k*=10; } num[0]=j; return *this; } node & node::operator = (int a) { char s[MAX]; sprintf(s,"%d",a); return *this=s; } node::node() { memset(num,0,sizeof(num)); num[0]=1; } node::node (int n) { *this = n; } bool node::operator > (const node &b) const { if (num[0]!=b.num[0]) return num[0]>b.num[0]; for (int i=num[0]; i>=1; i--) if (num[i]!=b.num[i]) return (num[i]>b.num[i]); return false; } bool node::operator < (const node &b) const { return b>*this; } bool node::operator <= (const node &b) const { return !(*this>b); } bool node::operator >= (const node &b) const { return !(b>*this); } bool node::operator != (const node &b) const { return (b>*this)||(*this>b); } bool node::operator == (const node &b) const { return !(b>*this)&&!(*this>b); } node node::operator + (const node &b) const { node c; c.num[0] = max(num[0], b.num[0]); for (int i=1; i<=c.num[0]; i++) { c.num[i]+=num[i]+b.num[i]; if (c.num[i]>=10000) { c.num[i]-=10000; c.num[i+1]++; } } if (c.num[c.num[0]+1]>0) c.num[0]++; return c; } node node::operator - (const node &b) const { node c; c.num[0] = num[0]; for (int i=1; i<=c.num[0]; i++) { c.num[i]+=num[i]-b.num[i]; if (c.num[i]<0) { c.num[i]+=10000; c.num[i+1]--; } } while (c.num[c.num[0]]==0&&c.num[0]>1) c.num[0]--; return c; } node & node::operator += (const node &b) { return *this=*this+b; } node & node::operator -= (const node &b) { return *this=*this-b; } node node::operator * (const node &b) const { node c; c.num[0] = num[0]+b.num[0]+1; for (int i=1; i<=num[0]; i++) { for (int j=1; j<=b.num[0]; j++) { c.num[i+j-1]+=num[i]*b.num[j]; c.num[i+j]+=c.num[i+j-1]/10000; c.num[i+j-1]%=10000; } } while (c.num[c.num[0]]==0&&c.num[0]>1) c.num[0]--; return c; } node & node::operator *= (const node &b) { return *this=*this*b; } node node::operator % (const node &b) const { node c, d; c.num[0] = num[0]+b.num[0]+1; d.num[0] = 0; for (int i=num[0]; i>=1; i--) { memmove(d.num+2, d.num+1, sizeof(d.num)-sizeof(int)*2); d.num[0]++; d.num[1]=num[i]; int left=0, right=9999, mid; while (left < right) { mid = (left+right)/2; if (b*node(mid) <= d) left=mid+1; else right=mid; } c.num[i]=right-1; d=d-b*node(right-1); } while (c.num[c.num[0]]==0&&c.num[0]>1) c.num[0]--; return d; } node & node::operator /= (const node &b) { return *this=*this/b; } node & node::operator %= (const node &b) { return *this=*this%b; } node node::operator / (const node& b) const { node c, d; c.num[0] = num[0]+b.num[0]+1; d.num[0] = 0; for (int i=num[0]; i>=1; i--) { memmove(d.num+2, d.num+1, sizeof(d.num)-sizeof(int)*2); d.num[0]++; d.num[1]=num[i]; int left=0, right=9999, mid; while (left < right) { mid = (left+right)/2; if (b*node(mid) <= d) left=mid+1; else right=mid; } c.num[i]=right-1; d=d-b*node(right-1); } while (c.num[c.num[0]]==0&&c.num[0]>1) c.num[0]--; return c; } ostream & operator << (ostream & o, node &n) { o<<n.num[n.num[0]]; for (int i=n.num[0]-1; i>=1; i--) { o.width(4); o.fill('0'); o<<n.num[i]; } return o; } istream & operator >> (istream & in, node &n) { char s[MAX]; in>>s; n=s; return in; }
原文地址:https://www.cnblogs.com/soledadstar/p/11516568.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 张东升,我知道是你!如何使用GAN做一个秃头生产器
- Roslyn 在 NuGet 包中放注释 xml 文件的方法
- OpenCV3 图像的加载、修改、显示与保存
- 打卡群刷题总结0727——搜索旋转排序数组 II
- dotnet 的 TaskCompletionSource 的 TrySetResult 是线程安全
- OpenCV 摄像头与视频读取并显示
- OpenCV 边缘检测
- Spring Boot 集成 WebSocket 实现服务端推送消息到客户端
- OpenCV 绘制图形
- OpenCV 图片缩放
- AI树莓派——构建树莓派大脑(NCNN环境搭建)
- PC人脸识别登录,出乎意料的简单
- TensorLayer学习之简介与安装
- JVM系列之:String.intern的性能
- 神经网络架构搜索——可微分搜索(DARTS)