0
点赞
收藏
分享

微信扫一扫

求最简分数


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;
}

举报

相关推荐

0 条评论