鱼C论坛

 找回密码
 立即注册
查看: 1890|回复: 1

求助一下

[复制链接]
发表于 2023-11-11 16:33:30 | 显示全部楼层
  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. void simplify(int &a, int &b) {
  9.     int temp = gcd(a, b);
  10.     a /= temp;
  11.     b /= temp;
  12. }

  13. int main() {
  14.     int a1, b1, a2, b2;
  15.     char slash;
  16.     cin >> a1 >> slash >> b1 >> a2 >> slash >> b2;

  17.     int numerator = a1 * b2 + a2 * b1;
  18.     int denominator = b1 * b2;
  19.     simplify(numerator, denominator);

  20.     if (denominator == 1) {
  21.         cout << numerator << endl;
  22.     } else {
  23.         cout << numerator << "/" << denominator << endl;
  24.     }

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

使用道具 举报

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

本版积分规则

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

GMT+8, 2025-10-11 05:50

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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