Expired License

Expired License


Expired License

这道题交给队友敲得,但是调试了一下午关于浮点数的精度老是出问题,参考了大佬的代码,终于知道怎么回事了

#include <iostream>
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b)
{
    return b == 0 ? a : gcd(b, a % b);
}
bool check(ll n)
{
    if(n == 1) return false;
    for(ll i = 2; i <= n / i; i++)
        if(n % i == 0)
            return false;
    return true;
}
int main()
{
    double a, b;
    int t;
    cin >> t;
    while(t--)
    {
        cin >> a >> b;
        if(a == b) cout << 2 << ' ' << 2 << endl;
        else
        {
            long long int n = (ll)(a * 100000.0 + 0.5), m = (ll)(b * 100000.0 + 0.5);//要加0.5,否则精度会丢失
            cout << n << ' ' << m << endl;
            ll e = gcd(n, m);
            if(check(n / e) && check(m / e))
                cout << n / e << ' ' << m / e << endl;
            else
                cout << "impossible\n";
        }
    }
    return 0;
}
posted @ 2022-05-02 19:46  Flying_bullet  阅读(34)  评论(0)    收藏  举报