求最简分数

Problem Description

 这天老师给小豪出了一道很简单题目放松:输入一个分数,让你求出它们的最简分数。

Input

 第一行包括一个T,表示测试数据的组数。

接下来T行每行包括一个分数。(分子分母均在int范围内)

Output

 对于每个测试样例,输出一行其最简分数。

Sample Input

3
2/6
3/1
11/36

Sample Output

1/3
3/1
11/36




#include <iostream>

using namespace std;
int mgcd(int a,int b)
{

    int t;
    if(a<b)
    {

        t=a;a=b;b=t;
    }
    while(a%b)
    {
        t=b;
        b=a%b;
        a=t;
    }
    return b;
}

int main()
{
    int a,b,t;
    char ch;
    int count=0;
    int T;
    cin>>T;
    while(cin>>a>>ch>>b)
    {
        ++count;
        t=mgcd(a,b);
        a /= t;
        b /= t;
        cout<<a<<"/"<<b<<endl;
        if(count==T)
        {
            break;
        }
    }

    return 0;
}


转载于:https://www.cnblogs.com/yldf/p/6249934.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值