当前位置:网站首页>信息学奥赛一本通 1209:分数求和 | OpenJudge 1.13 12:分数求和
信息学奥赛一本通 1209:分数求和 | OpenJudge 1.13 12:分数求和
2022-04-21 07:04:00 【君义_noip】
【题目链接】
ybt 1209:分数求和
OpenJudge 1.13 12:分数求和
【题目考点】
1. 求最大公约数
2. 求最小公倍数
【解题思路】
求最大公约数,可以用辗转相除法。具体方法见信息学奥赛一本通 1207:求最大公约数问题 | OpenJudge 2.2 7592:求最大公约数问题
两数的最小公倍数为两数乘积除以两数的最大公约数。
解法1:基本解法
n个分数加和,结果的分母为所有分数分母的最小公倍数,每个分数的分子扩大相应倍数加和为结果的分子。
最后结果的分子和分母除以二者的最大公约数,即可得到最简形式。
解法2:将分数写成类
分数类中包含分子、分母,以及两分数相加的成员函数。
设两分数为 a b \frac{a}{b} ba与 c d \frac{c}{d} dc,二者相加的过程为:
- a b + c d = a ⋅ d + b ⋅ c b ⋅ d \frac{a}{b}+\frac{c}{d}=\frac{a\cdot d+b\cdot c}{b\cdot d} ba+dc=b⋅da⋅d+b⋅c
- 分子分母除以二者的最大公约数,得到最简形式。
输入每个分数成为一个分数类的对象,多个分数做加和。
【题解代码】
解法1:基本解法
#include<bits/stdc++.h>
using namespace std;
#define N 15
int gcd(int a, int b)//求a与b的最大公约数
{
if(b == 0)
return a;
return gcd(b, a%b);
}
int lcm(int a, int b)//求a与b的最小公倍数
{
return a*b/gcd(a, b);
}
int main()
{
int n, a[N], b[N], m = 1, d = 0, g;//a[i]:第i个分数的分子 b[i]:第i个分数的分母
char c;
cin >> n;
for(int i = 1; i <= n; ++i)
cin >> a[i] >> c >> b[i];//字符变量c用于吸收除号
for(int i = 1; i <= n; ++i)//求所有分母的最小公倍数为m
m = lcm(m, b[i]);
for(int i = 1; i <= n; ++i)//所有分数都变成分母为m,此时所有分子的加和为d
d += m/b[i]*a[i];
g = gcd(d, m);//g为分子与分母的最大公约数
cout << d/g;//输出分子
if(m/g > 1)//如果分母大于1,则输出分母
cout << '/' << m/g;
return 0;
}
解法2:将分数写成类
#include<bits/stdc++.h>
using namespace std;
#define N 15
int gcd(int a, int b)//求a与b的最大公约数
{
if(b == 0)
return a;
return gcd(b, a%b);
}
struct Frac//分数类
{
int d, m;//d:分子 m:分母
Frac operator + (Frac &b)//重载加号运算符。当然也可以直接写一个求分数相加的函数。
{
Frac r;
int c;
r.m = m*b.m;//结果的分母是两分数分母相乘
r.d = d*b.m + b.d*m;//在分母是r.m的情况下,分子相加
c = gcd(r.m, r.d);//求分子分母的最大公约数
r.m /= c;
r.d /= c;
return r;
}
void show()
{
if(m == 1)
cout << d;
else
cout << d << '/' << m;
}
};
Frac f[N], ans;
int main()
{
int n;
char c;
cin >> n;
ans.d = 0, ans.m = 1;//将ans设为 0/1,分母必须不能为0。
for(int i = 1; i <= n; ++i)
{
cin >> f[i].d >> c >> f[i].m;
ans = ans + f[i];
}
ans.show();
return 0;
}
版权声明
本文为[君义_noip]所创,转载请带上原文链接,感谢
https://blog.csdn.net/lq1990717/article/details/124311829
边栏推荐
- C# asp. Net calling Baidu character recognition interface
- 2022年危险化学品生产单位安全生产管理人员考试模拟100题模拟考试平台操作
- “产业数字化”的技术背景与现实作用
- Playwright, selenium, operation ifram element
- 迅为i.MX6Q开发板Openwrt 文件系统构建
- Gateway and distributed ID
- IIC bus design ① - IIC communication protocol
- The fifth stop is the hometown of Confucius and Mencius ------- the shortest path to the maze
- 如何修改 Rancher Server 的 IP 地址
- Technical background and practical role of "industrial digitization"
猜你喜欢

Technical background and practical role of "industrial digitization"

Hackmyvm综合靶机 | Driftingblues-9(完结)

Advanced C language pointer (1. First and second order pointers)

实战JDBC连接Mysql数据库

Couchdb-垂直越权

无意中发现了一位清华妹子的资料库!

Renesas ide: development environment configuration during bootloader upgrade of CS + for CC

Flutter environment construction and other foundations

Neptune w800点灯(中断)工程详解

MySQL5. 7 installation and operation manual (centos7)
随机推荐
PHP Chinese to English initials
树的基本概念与存储结构
It can switch the aircraft duel with three interfaces of start, battle and settlement, modularization, pyGame, rect and exe packaging
Set Google chrome dark black background
Shortcut keys for batch modification of variable name and batch replacement code in idea
Renesas ide: development environment configuration during bootloader upgrade of CS + for CC
C WebService interface obtains JSON parameters through request
Why is the primary key matching setting in MySQL showing that I am wrong?
Execute rule after MySQL where condition
如何修改 Rancher Server 的 IP 地址
Self made webserver from scratch (XV) -- the log base part is completed, and the practical widget double-buffering optimizes the asynchronous write performance
Apache-Skywalking-SQL注入(CVE-2020-9483)复现
V-has permission control based on jeecgboot
Bluetooth profile specification (AVRCP chapter) 5.1 connection and release of vctp
Laravel packages multiple files and downloads them
Foundation of golang project development
J'ai accidentellement trouvé la base de données d'une soeur Tsinghua!
Introduction to QT 5.12 actual combat
leetcode题目--386.字典序排数,DFS
显示器选购参考天梯