关闭

浙大PAT甲级 1088

60人阅读 评论(0) 收藏 举报
分类:

考虑的情况比较多:

(1) 除数为0则,则等号后面输出inf。

(2)求最小公倍数时,两数相乘的范围可能超过了int所表示的范围,因此应选用long long 类型。

AC代码:

#include<iostream>
#include<map>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<list>
#include<set>
#include<stack>
#include<cmath>
#include<vector>
#define ll long long
#define inf 999999999
using namespace std;
ll gcd(ll x,ll y)
{
    return x%y==0?y:gcd(y,x%y);
}
ll f(ll x,ll y)
{
  return x*y/gcd(x,y);
}
void print(ll x,ll y)
{
    if(y<0)
    {
        x=-x;
        y=-y;
    }
    if(x==0)
    {
        printf("0");
    }
    else if(x>0)
    {
        ll r=gcd(x,y);
        x/=r;
        y/=r;
        if(y==1)
        {
            printf("%lld",x);
            return;
        }
        if(x/y==0)
        {
            printf("%lld/%lld",x,y);
        }
        else
        {
            ll d=x/y;
            printf("%lld %lld/%lld",d,x-d*y,y);
        }
    }
    else
    {
        printf("(-");
        x=-x;
        ll r=gcd(x,y);
        x/=r;
        y/=r;
        if(y==1)
        {
            printf("%lld)",x);
            return;
        }
        if(x/y==0)
        {
            printf("%lld/%lld",x,y);
        }
        else
        {
            ll d=x/y;
            printf("%lld %lld/%lld",d,x-d*y,y);
        }
        printf(")");
    }
}
int main()
{
    string s1;
    string s2;
    cin>>s1>>s2;
    int flag1=0;
    if(s1[0]=='-')
    {
        flag1=1;
        s1.erase(0,1);
    }
    int flag2=0;
    if(s2[0]=='-')
    {
        flag2=1;
        s2.erase(0,1);
    }
    ll mark1;
    ll mark2;
    ll sum=0;
    ll fenzi1;
    ll fenmu1;
    ll fenzi2;
    ll fenmu2;
    for(ll i=0;i<s1.size();i++)
    {
        if(s1[i]=='/')
        {
            mark1=i;
            break;
        }
        sum*=10;
        sum+=s1[i]-'0';
    }
    fenzi1=flag1==1?-sum:sum;
    sum=0;
    for(ll i=mark1+1;i<s1.size();i++)
    {
        sum*=10;
        sum+=s1[i]-'0';
    }
    fenmu1=sum;
    if(fenzi1==0)
    {
        fenmu1=1;
    }
    sum=0;
    for(ll i=0;i<s2.size();i++)
    {
        if(s2[i]=='/')
        {
            mark2=i;
            break;
        }
        sum*=10;
        sum+=s2[i]-'0';
    }
    fenzi2=flag2==1?-sum:sum;
    sum=0;
    for(ll i=mark2+1;i<s2.size();i++)
    {
        sum*=10;
        sum+=s2[i]-'0';
    }
    fenmu2=sum;
    if(fenzi2==0)
    {
        fenmu2=1;
    }
    ll fz1=fenzi1;
    ll fm1=fenmu1;
    ll fz2=fenzi2;
    ll fm2=fenmu2;
    print(fenzi1,fenmu1);
    printf(" + ");
    print(fenzi2,fenmu2);
    printf(" = ");
    ll h=f(fenmu1,fenmu2);
    fenzi1=h/fenmu1*fenzi1;
    fenzi2=h/fenmu2*fenzi2;
    ll fenmu=h;
    ll fenzi=fenzi1+fenzi2;
    print(fenzi,fenmu);
    printf("\n");
    fenzi1=fz1;
    fenzi2=fz2;
    fenmu1=fm1;
    fenmu2=fm2;
    print(fenzi1,fenmu1);
    printf(" - ");
    print(fenzi2,fenmu2);
    printf(" = ");
     h=f(fenmu1,fenmu2);
    fenzi1=h/fenmu1*fenzi1;
    fenzi2=h/fenmu2*fenzi2;
     fenmu=h;
     fenzi=fenzi1-fenzi2;
    print(fenzi,fenmu);
    printf("\n");
 fenzi1=fz1;
    fenzi2=fz2;
    fenmu1=fm1;
    fenmu2=fm2;

    print(fenzi1,fenmu1);
    printf(" * ");
    print(fenzi2,fenmu2);
    printf(" = ");
     fenmu=fenmu1*fenmu2;
     fenzi=fenzi1*fenzi2;
    print(fenzi,fenmu);
    printf("\n");

     fenzi1=fz1;
    fenzi2=fz2;
    fenmu1=fm1;
    fenmu2=fm2;
    print(fenzi1,fenmu1);
    printf(" / ");
    print(fenzi2,fenmu2);
    printf(" = ");
    if(fenzi2==0)
    {
        printf("Inf\n");
        return 0;
    }
     fenmu=fenmu1*fenzi2;
     fenzi=fenzi1*fenmu2;
    print(fenzi,fenmu);
    printf("\n");

}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:10220次
    • 积分:960
    • 等级:
    • 排名:千里之外
    • 原创:91篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档