Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
1.1 kB
5
Indexable
Never
#include <iostream>
#include <iomanip>
using namespace std;
#define MAXN 200
typedef int PhanSo;

int UCLN(int a, int b)
{
    if (b == 0) return a;
    else
        return UCLN(b, a % b);
}

void Nhap(int a[MAXN], int& n)
{
    cin >> n;
    for (int i = 0; i < n * 2; i++) {
        cin >> a[i];
    }
}
void Xuat(int a[MAXN], int& n)
{
    for (int i = 0; i < n * 2; i += 2)
    {
        int tu = a[i];
        int mau = a[i + 1];
        if (mau == 0) cout << "Khong thoa yeu cau bai toan \n";
        else
        {
            int ucln = UCLN(abs(tu), abs(mau));
            if (ucln != 1)
            {
                tu /= ucln;
                mau /= ucln;
            }
            if (tu == 0) cout << "0 \n";
            else if (mau == 1) cout << tu << endl;
            else
            {
                if ((tu > 0 && mau < 0) || (tu < 0 && mau > 0))
                    cout << "-";
                cout << abs(tu) << "/" << abs(mau) << endl;
            }
        }
    }
}
int main() {
    PhanSo a[MAXN];
    int n;
    Nhap(a, n);
    Xuat(a, n);
    return 0;
}
Leave a Comment