标题:又是一道错误的答案题,明明测试数据都过了。。。。。。。。。
只看楼主
qazujm1212
Rank: 2
等 级:论坛游民
帖 子:24
专家分:24
注 册:2011-3-23
得分:10 
回复 楼主 b465513006
#include <stdio.h>
__int64 yue(__int64 a, __int64 b)
{
    if (a==0 || b==0)
        return 0;
    else if (a%b==0)
        return b;
    else
        return yue(b,a%b);
}
__int64 bei(__int64 a,__int64 b)
{
    return a*b/yue(a,b);   
}

void pt( __int64 a , __int64 b)
{
    if (a==0)
        printf ("0/1");
    if (b==0)
        printf ("INF");
    if (a<0 && b<0 || a>0 && b<0)
        printf ("%I64d/%I64d",-a,-b);
    if (a<0 && b>0  || a>0 &&b>0)
        printf ("%I64d/%I64d",a,b);
}

int main()
{   
    //freopen ("asd.in","r",stdin);
    //freopen ("asd.out","w",stdout);
    __int64 a,b,c,d,n,x,m,k;
    int i,j;
    char p;
    scanf ("%d",&j);
    for (i=0;i<j;i++)
    {
        scanf ("%I64d/%I64d %c %I64d/%I64d",&a,&b,&p,&c,&d);
        if (p=='+')
        {
            x=bei(b,d);
            m=x/b*a;
            n=x/d*c;
            k=yue(m+n,x);
            if (k==0)
                pt((m+n),x);
            else if (x/k==0)
                printf ("INF");
            else   
                pt((m+n)/k,x/k);
        }
        else if (p=='-')
        {
            x=bei(b,d);
            m=x/b*a;
            n=x/d*c;
            k=yue(m-n,x);
            if (k==0)
                pt((m-n),x);
            else if (x/k==0)
                printf ("INF");
            else   
                pt((m-n)/k,x/k);
        }
        else if (p=='*')
        {
            m=a*c;
            n=b*d;
            k=yue (m,n);
            if (k==0)
                pt(m,n);
            else if (n==0)
                printf ("INF");
            else
                pt(m/k,n/k);
        }
        else if (p=='/')
        {
            m=a*d;
            n=b*c;
            k=yue (m,n);
            if (n==0)
                printf ("INF");
            else if (k==0)
                pt(m,n);
            else
                pt(m/k,n/k);
        }
        printf ("\n");
    }
    return 0;
}


2011-08-09 09:59



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-347074-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.350239 second(s), 8 queries.
Copyright©2004-2025, BCCN.NET, All Rights Reserved