分数比较大小

业余的业余连专业选手的尾气都闻不到,难受

题目描述
Bobo has two fractions a/x and b/y. He wants to compare them. Find the result.

输入描述:
The input consists of several test cases and is terminated by end-of-file.

Each test case contains four integers x, a, y, b.

0 <= x, y <= 10^18
1 <= a,b <= 10^9
There are at most 10^5 test cases.

输出描述:
For each test case
if x/a = y/b,Print ‘=’
if x/a < y/b,Print ‘<’
if x/a > y/b, Print ‘>’

示例1
输入

1 2 1 1
1 1 1 2
1 1 1 1

输出

<
>
=

题目目的很明确,比较2个分数的大小,但是有大数,注意边界

所以模拟一下分数,然后比较。 思路就是 x / a 与 y / b 的大小其实是 x * b 与 y * a 的大小,但是大数会溢出,所以可以取余。假设:
x / a = m + x1/a;
y / b = n + y1/b;

如果n>m, y/b > x/a,反之同理,如果相等,就比较余数即可。

#include<iostream>
using namespace std;
using ll = long long;
int main()
{
    ll x,a,y,b;  
    while(cin>>x>>a>>y>>b){
    ll x1 = x/a;
    ll y1 = y/b;
    
    int flag = 0;
    if(x1 > y1) flag = 1;
    else if(x1 < y1) flag = -1;
    else if(x1 == y1)
    {
        ll x2 = x%a;
        ll y2 = y%b;
        
        x2 *= b;
        y2 *= a;
        if(x2 == y2) flag = 0;
        else if(x2 > y2) flag = 1;
        else flag = -1;
    }
    if(flag == 0)
    {
        cout<<'='<<endl;
    }
    else if(flag == 1)
    {
        cout<<'>'<<endl;
    }
    else
    {
        cout<<'<'<<endl;
    }
}
    return 0;
}
### C++ 实现分数比较 为了实现两个分数之间的比较,在C++中可以创建一个`Rational`类来表示有理数,并重载关系运算符以便于进行比较。以下是基于此思路的一个简单实现: ```cpp #include <iostream> using namespace std; class Rational { private: int numerator; int denominator; public: // 构造函数初始化分子和分母,默认值为0/1 Rational(int num = 0, int deno = 1) : numerator(num), denominator(deno) {} // 获取最大公约数辅助方法 static int gcd(int a, int b) { while (b != 0) { int temp = b; b = a % b; a = temp; } return a; } // 约简分数 void reduce() { if (denominator < 0) { // 处理负分母的情况 numerator = -numerator; denominator = -denominator; } int divisor = gcd(abs(numerator), abs(denominator)); numerator /= divisor; denominator /= divisor; } // 设置成员变量的值 void set(int n, int d) { numerator = n; denominator = d; reduce(); } // 打印当前对象所代表的分数 friend ostream& operator<<(ostream&, const Rational&); // 比较两个分数是否相等 bool operator==(const Rational&) const; // 小于号重载,用于判断左侧小于右侧 bool operator<(const Rational&) const; // 不等于符号重载 bool operator!=(const Rational& other) const { return !(*this == other); } // 大于号重载 bool operator>(const Rational& other) const { return other < *this; } // 小于等于号重载 bool operator<=(const Rational& other) const { return !(other < *this); } // 大于等于号重载 bool operator>=(const Rational& other) const { return !(*this < other); } }; // 输出流重载,方便打印分数形式的结果 ostream& operator<<(ostream& os, const Rational& r) { os << r.numerator << "/" << r.denominator; return os; } bool Rational::operator==(const Rational& rhs) const { return this->numerator * rhs.denominator == rhs.numerator * this->denominator; } bool Rational::operator<(const Rational& rhs) const { return this->numerator * rhs.denominator < rhs.numerator * this->denominator; } ``` 上述代码展示了如何定义一个能够执行基本算术运算以及相互之间比较大小的`Rational`类[^4]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值