10.公比

#include<iostream>
#include<algorithm>
using namespace std;
struct node
{
    long long x,y;
    double c;
} s1[1000],s2[1000];
bool comp(node x, node y)
{
    return x.c < y.c;
}
long long gcd(long long x, long long y)
{
    if(y == 0)
        return x;
    else
        gcd(y, x%y);

}
int quchong(int n, node *s)
{
    int j=0;
    node temp[1000];
    bool judge[1000];
    for(int i=0; i < n; i++)
    {
        temp[i].x = s[i].x;
        temp[i].y = s[i].y;
        temp[i].c = s[i].c;
        judge[i] = false;
    }
    for(int i=0; i<n-1; i++)
    {
        if(judge[i] == 0)
            s[j++] = temp[i];
        if(temp[i].c == temp[i+1].c)
            judge[i+1] = true;
    }
    if(temp[n-2].c!= temp[n-1].c)
    {
        s[j] = temp[n-1];
        j++;
    }
    return j;
}
node Operator(int n)
{
    int judge = 1,n2=0;
    long long a,b,c,d,g1,g2;
    while(n != 1 && n2 != 1)
    {
        if(judge == 1)
        {
            sort(s1,s1+n,comp);
            n=quchong(n,s1);
            int i;
            for(i=0; i<n-1; i++)
            {
                g1 = gcd(s1[i].x, s1[i+1].x);
                g2 = gcd(s1[i].y, s1[i+1].y);
                a = s1[i].x/g1;
                b = s1[i+1].x/g1;
                c = s1[i].y/g2;
                d = s1[i+1].y/g2;
                s2[i].x = b * c;
                s2[i].y = a * d;
                s2[i].c = s2[i].x / s2[i].y;
            }
            judge = 0;
            n2 = i;
        }
        else
        {
            sort(s2,s2+n2,comp);
            n2=quchong(n2,s2);
            int i;
            for(i=0; i<n2-1; i++)
            {
                g1 = gcd(s2[i].x, s2[i+1].x);
                g2 = gcd(s2[i].y, s2[i+1].y);
                a = s2[i].x/g1;
                b = s2[i+1].x/g1;
                c = s2[i].y/g2;
                d = s2[i+1].y/g2;
                s1[i].x = b * c;
                s1[i].y = a * d;
                s1[i].c = s1[i].x / s1[i].y;
            }
            judge = 1;
            n = i;
        }

    }
    if(n == 1)
        return s1[0];
    else
        return s2[0];
}
int main()
{
    int n;
    node temp;
    cin>>n;
    for(int i=0; i<n; i++)
    {
        long long temp;
        cin>>temp;
        s1[i].x = temp;
        s1[i].y = 1;
        s1[i].c = temp/1;
    }
    temp = Operator(n);
    cout<<temp.x<<"/"<<temp.y<<endl;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值