Thứ Sáu, 13 tháng 5, 2022

BIEUTHUC1235 C++

#include <bits/stdc++.h>
using namespace std;

struct ps
{
    int t,m;
};

int n;
ps res, p[1000],a[1000];
char dau[100];
int d=0;

ps cong(ps a,ps b)
{
   // cout<<"cong1: "<<a.t<<" "<<a.m<<endl;
 //    cout<<"cong2: "<<b.t<<" "<<b.m<<endl;
    ps c;
    int ucln=__gcd(a.m,b.m);
    int mc=(a.m*b.m)/ucln;
  //  cout<<mc;
    c.m=mc;
    c.t=a.t*(mc/a.m)+b.t*(mc/b.m);
  //  cout<<"cong: "<<c.t<<" "<<c.m<<endl;
    return c;
}

ps nhan(ps a,ps b)
{
    ps c;
    c.m=a.m*b.m;
    c.t=a.t*b.t;
    return c;
}

ps toigian(ps a)
{
    int ucln=__gcd(a.t,a.m);
    ps b;
    b.t=a.t/ucln;
    b.m=a.m/ucln;
    if(b.m<0)
    {
        b.t=-b.t;
        b.m=-b.m;
    }
    return b;
}
void tien_xu_li()
{
    a[1]=p[1];d=1;
    for(int i=1;i<=n-1;i++)
    {
        if(dau[i]=='+')
        {
            d++;
            a[d]=p[i+1];
        }
        if(dau[i]=='-')
        {
            d++;
            p[i+1].t=-p[i+1].t;
            a[d]=p[i+1];
        }
        if(dau[i]=='*')
        {
            a[d]=nhan(a[d],p[i+1]);
        }
    }
}


void tinh()
{
    ps res=a[1];
  //  cout<<a[1].t<<" "<<a[1].m<<"\n";
    for(int i=2;i<=d;i++)
    {
    //    cout<<a[i].t<<" "<<a[i].m<<"\n";
        res=cong(res,a[i]);
    }
    res=toigian(res);
    cout<<res.t<<" "<<res.m;
}

void nhap()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        cin >> p[i].t >> p[i].m;
    }
     for(int i = 1; i <= n-1; i++)
    {
        cin >> dau[i];
    }
}

int main()
{
    freopen("BIEUTHUC1235.inp","r",stdin);
    freopen("BIEUTHUC1235.out","w",stdout);
    nhap();
    tien_xu_li();
    tinh();
}

Không có nhận xét nào:

Đăng nhận xét

Lưu ý: Chỉ thành viên của blog này mới được đăng nhận xét.