尧图网站建设 尧图网络
  • 首页
  • 关于我们
  • 服务项目
  • 案例展示
  • 建站流程
  • 资讯中心
  • 联系我们
首页/资讯中心/详情

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

(c++)算法竞赛用,分数类模板
📅 发布时间:2026/6/22 2:19:13

rt

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

\(a\) 是分子,\(b\) 是分母,如果 b=0 会 exit(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;
}

相关新闻

  • 学习图片数据如何处理为MindSpore自定义数据集
  • Java程序员该如何快速上手LLM应用开发呢?
  • 经典欧拉角与泰特布莱恩角

最新新闻

  • 嵌入式调试利器MMDS0508:实时总线分析与非侵入式仿真实战
  • 保定卖多拉拉货车的官方授权店,货拉拉在河北省的省级总代理,核心业务与特色服务 - 企业品牌
  • 2026年漯河合同纠纷律师选对=省心 张骁隆律师值得推荐(附联系方式) - 本地品牌推荐
  • 保定哪里有卖多拉3米8,卖货拉拉货车官方授权店,货拉拉新能源汽车河北省省级总代理 - 企业品牌
  • Seedance 2.0:结构化视频生成引擎与分层可控架构解析
  • R3nzSkin英雄联盟换肤工具:免费体验全皮肤的终极指南

日新闻

  • 2026速览惠州叛逆青少年学校前十大排名名单出炉 - 武汉中职最新信息发布
  • 2026上饶白蚁消杀哪家好?15年本土2大权威白蚁防治公司推荐(金盾虫控/青蚁卫士) - 我叫一
  • 天龙八部单机版终极数据管理工具:5个技巧快速掌握游戏数据编辑

周新闻

  • Visual C++运行库修复终极指南:5分钟快速解决Windows软件启动错误
  • 手把手教你构建统计局地区经济数据爬虫:从环境搭建到数据持久化全指南
  • 2026多Agent深度解析:用AI团队替代单一模型,四种架构实战落地

月新闻

  • 【总结】入门篇:50句话让你记住架构核心概念
  • WeChatMsg技术方案解析:实现Mac微信数据自主管理的完整解决方案
  • WeChatMsg:革新性微信数据备份方案,打造你的专属数字记忆库

关于尧图

  • 公司简介
  • 团队介绍
  • 企业文化
  • 荣誉资质

服务项目

  • 定制开发
  • 电商建站
  • UI 设计
  • 运维服务

快速链接

  • 案例展示
  • 建站流程
  • 常见问题
  • 资讯中心

联系方式

  • 📍北京市朝阳区互联网产业园 A 座 10 层
  • 📞400-888-8888
  • ✉️contact@rkmt.cn
  • 🕐周一至周日 9:00-21:00

© 2024 北京尧图网络科技有限公司 版权所有 | 京 ICP 备 XXXXXXXX 号