鱼C论坛

 找回密码
 立即注册
查看: 1322|回复: 2

[已解决]求助一下

[复制链接]
发表于 2023-11-12 18:54:30 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
7-7 有理数加法
分数 10
作者 乔林
单位 清华大学
本题要求编写程序,计算两个有理数的和。

输入格式:
输入在一行中按照a1/b1 a2/b2的格式给出两个分数形式的有理数,其中分子和分母全是整形范围内的正整数。

输出格式:
在一行中按照a/b的格式输出两个有理数的和。注意必须是该有理数的最简分数形式,若分母为1,则只输出分子。

输入样例1:
1/3 1/6
输出样例1:
1/2
输入样例2:
4/3 2/3
输出样例2:
2

这题用C语言或者C++怎么做
最佳答案
2023-11-12 18:54:35
  1. #include <iostream>
  2. using namespace std;

  3. // 求最大公约数
  4. int gcd(int a, int b) {
  5.     return b == 0 ? a : gcd(b, a % b);
  6. }

  7. int main() {
  8.     int a1, b1, a2, b2;
  9.     char op;
  10.     cin >> a1 >> op >> b1 >> a2 >> op >> b2;

  11.     int up = a1 * b2 + a2 * b1;
  12.     int down = b1 * b2;
  13.     int d = gcd(abs(up), abs(down));

  14.     up /= d;
  15.     down /= d;

  16.     if (down == 1) {
  17.         cout << up << endl;
  18.     } else {
  19.         cout << up << '/' << down << endl;
  20.     }

  21.     return 0;
  22. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2023-11-12 18:54:35 | 显示全部楼层    本楼为最佳答案   
  1. #include <iostream>
  2. using namespace std;

  3. // 求最大公约数
  4. int gcd(int a, int b) {
  5.     return b == 0 ? a : gcd(b, a % b);
  6. }

  7. int main() {
  8.     int a1, b1, a2, b2;
  9.     char op;
  10.     cin >> a1 >> op >> b1 >> a2 >> op >> b2;

  11.     int up = a1 * b2 + a2 * b1;
  12.     int down = b1 * b2;
  13.     int d = gcd(abs(up), abs(down));

  14.     up /= d;
  15.     down /= d;

  16.     if (down == 1) {
  17.         cout << up << endl;
  18.     } else {
  19.         cout << up << '/' << down << endl;
  20.     }

  21.     return 0;
  22. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2023-11-12 18:55:38 | 显示全部楼层
为了计算两个有理数的和,你可以使用C++编写一个简单的程序。以下是一个示例代码:
  1. #include <iostream>
  2. using namespace std;
  3. // 求最大公约数
  4. int gcd(int a, int b) {
  5.     return b == 0 ? a : gcd(b, a % b);
  6. }
  7. // 定义结构体表示有理数
  8. struct Rational {
  9.     int numerator; // 分子
  10.     int denominator; // 分母
  11. };
  12. // 计算两个有理数的和
  13. Rational addRational(const Rational& r1, const Rational& r2) {
  14.     Rational result;
  15.     result.numerator = r1.numerator * r2.denominator + r2.numerator * r1.denominator;
  16.     result.denominator = r1.denominator * r2.denominator;
  17.     // 简化分数
  18.     int common = gcd(result.numerator, result.denominator);
  19.     result.numerator /= common;
  20.     result.denominator /= common;
  21.     return result;
  22. }
  23. int main() {
  24.     Rational r1, r2;
  25.     char slash;
  26.     cin >> r1.numerator >> slash >> r1.denominator >> r2.numerator >> slash >> r2.denominator;
  27.     Rational result = addRational(r1, r2);
  28.     if (result.denominator == 1) {
  29.         cout << result.numerator << endl;
  30.     } else {
  31.         cout << result.numerator << "/" << result.denominator << endl;
  32.     }
  33.     return 0;
  34. }
复制代码

在这个示例代码中,我们定义了一个Rational结构体用来表示有理数,并实现了两个函数。gcd 函数用来求最大公约数,addRational 函数用来计算两个有理数的和并简化结果。在 main 函数中,我们读入两个有理数,调用 addRational 函数计算它们的和,并输出最简形式的结果。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-22 03:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表