当前位置: 首页 > news >正文

(c++)算法竞赛用,分数类模板

rt

支持分数间加减乘除,以及分数和小数的加减乘除,还有输入输出重载。

\(a\) 是分子,\(b\) 是分母,如果 b=0exit(486) 表示错误。

分数的符号以分子的正负号表示。

可以用这个去水掉烂题 NOIP2020T1(记得开__int128)。

code

#include <bits/stdc++.h>
//#define int int64_t
//#define int __int128
//#define MOD (1000000007)
//#define eps (1e-6)
#define endl '\n'
#define debug_endl cout<<endl;
#define debug cout<<"debug"<<endl;
using namespace std;
struct Rnumber{int a,b;inline Rnumber(){a=0,b=1;}inline Rnumber(int _a,int _b){a=_a,b=_b;}inline void limit(int &_a,int &_b){//内置化简/统一分数格式if(_b<0) _a=-_a,_b=-_b;		//正负号统一在分子上if(b==0) exit(486);			//b=0不是个实数,返回错误int flag=(_a<0?-1:1);		//以免gcd输入负数int t=__gcd(_a*flag,_b);	//分数化简_a/=t,_b/=t;_a*=flag;}//重载运算符inline Rnumber operator+(const Rnumber& other){//分数+分数int _a=a*other.b+other.a*b,_b=other.b*b;limit(_a,_b);return Rnumber(_a,_b);}inline Rnumber operator-(const Rnumber& other){//分数-分数int _a=a*other.b-other.a*b,_b=other.b*b;limit(_a,_b);return Rnumber(_a,_b);}inline Rnumber operator*(const Rnumber& other){//分数*分数int _a=a*other.a,_b=other.b*b;limit(_a,_b);return Rnumber(_a,_b);}inline Rnumber operator/(const Rnumber& other){//分数/分数int _a=a*other.b,_b=other.a*b;limit(_a,_b);return Rnumber(_a,_b);}inline Rnumber operator+(const int& other){//分数+整数int _a=a+other*b,_b=b;limit(_a,_b);return Rnumber(_a,_b);}inline Rnumber operator-(const int& other){//分数-整数int _a=a-other*b,_b=b;limit(_a,_b);return Rnumber(_a,_b);}inline Rnumber operator*(const int& other){//分数*整数int _a=a*other,_b=b;limit(_a,_b);return Rnumber(_a,_b);}inline Rnumber operator/(const int& other){//分数/整数int _a=a,_b=b*other;limit(_a,_b);return Rnumber(_a,_b);}//以下对应上面基本二元运算符的一元形式inline Rnumber operator+=(const Rnumber& other){*this=*this+other;return *this;}inline Rnumber operator-=(const Rnumber& other){*this=*this-other;return *this;}inline Rnumber operator*=(const Rnumber& other){*this=*this*other;return *this;}inline Rnumber operator/=(const Rnumber& other){*this=*this/other;return *this;}inline Rnumber operator+=(const int& other){*this=*this+other;return *this;}inline Rnumber operator-=(const int& other){*this=*this-other;return *this;}inline Rnumber operator*=(const int& other){*this=*this*other;return *this;}inline Rnumber operator/=(const int& other){*this=*this/other;return *this;}friend std::istream& operator>>(std::istream& is, Rnumber& f) {//重载输入is>>f.a>>f.b;return is;}friend std::ostream& operator<<(std::ostream& os, const Rnumber& f) {//重载输出os<<f.a<<' '<<f.b;return os;}
};
signed main(){//freopen(".in","r",stdin);//freopen(".out","w",stdout);Rnumber a,b;int c;cout<<"输入分数a:";cin>>a;cout<<"输入分数b:";cin>>b;cout<<"输入整数c:";cin>>c;cout<<"你输入的分数为:"<<a<<endl<<b<<endl;cout<<"a+b:"<<a+b<<endl;cout<<"a-b:"<<a-b<<endl;cout<<"a*b:"<<a*b<<endl;cout<<"a/b:"<<a/b<<endl;cout<<"a+c:"<<a+c<<endl;cout<<"a-c:"<<a-c<<endl;cout<<"a*c:"<<a*c<<endl;cout<<"a/c:"<<a/c<<endl;return 0;
}
http://www.rkmt.cn/news/43344.html

相关文章:

  • 学习图片数据如何处理为MindSpore自定义数据集
  • Java程序员该如何快速上手LLM应用开发呢?
  • 经典欧拉角与泰特布莱恩角
  • 详细介绍:识别含地理信息的 PDF作为底图在 Mapbox GL项目中使用
  • Markdown使用
  • Apifox接口测试工具简介 - 努力-
  • 定金单专题
  • CJI8运行查询没有数据
  • 贪心,排序,二分,分治
  • python01
  • AI Compass前沿速览:Cursor 2.0、Firefly Image5、Agent HQ 、LongCat-Video、Kimi-k2 Thinking
  • 25.11.7联考题解
  • EPnP算法学习随笔
  • 【机器学习入门】7.1 决策树 —— 像 “判断流程图” 一样做分类 - 教程
  • 完整教程:Labview项目01:标准可配置序列测试框架
  • Transformer Decoder 中序列掩码(Sequence Mask / Look-ahead Mask) - 详解
  • P9785 [ROIR 2020] 对常规的斗争 (Day1) 题解
  • 深入解析:SciPy傅里叶变换与信号处理教程:数学原理与Python实现
  • CentOS Stream 9编译安装Nginx 1.28 - Leone
  • JavaWeb03-Vue
  • 调整包含特定文本的单元格所在的行高
  • 一次十分折腾的系统迁移:BCD损坏(0xc000000f), 0xc0000255, 0xc000000e以及解决办法
  • 2025昆山/太仓/苏州/常熟/上海/农村自建房推荐榜 巨德翔建筑领衔 三家实力公司赋能乡村宜居生活
  • 深入解析:ST-Raptor:无需微调,准确率超越 GPT-4o 的半结构化表格问答新范式
  • 树上拓扑序个数小记
  • 2023最新Win10/Win11运行罪恶都市解决方案
  • 2025废气处理/废气治理/环保/污水/分子筛/除臭设备推荐榜:深城环保五星领跑,3 家企业以技术适配解锁多元异味治理场景
  • P6954 [NEERC 2017] Connections 题解
  • CF1463E Plan of Lectures
  • 2025年防水补漏企业TOP5:漏水维修、防水翻新、漏水检测