hdu1717 小数化分数2

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1717

小数化分数2

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2921    Accepted Submission(s): 1187


Problem Description
Ray 在数学课上听老师说,任何小数都能表示成分数的形式,他开始了化了起来,很快他就完成了,但他又想到一个问题,如何把一个循环小数化成分数呢?
请你写一个程序不但可以将普通小数化成最简分数,也可以把循环小数化成最简分数。
 

Input
第一行是一个整数N,表示有多少组数据。
每组数据只有一个纯小数,也就是整数部分为0。小数的位数不超过9位,循环部分用()括起来。
 

Output
对每一个对应的小数化成最简分数后输出,占一行。
 

Sample Input
  
  
3 0.(4) 0.5 0.32(692307)
 

Sample Output
  
  
4/9 1/2 17/52
 

Source
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   1715  1716  1166  1719  1722 
 

这个题目参见百度

http://zhidao.baidu.com/link?url=cJ2IRdzhGzhB3Ct4l0hic1K31tF-tKeb5oI0YlKE5huks_FNDHsNYEfXPfvV1yZTH9XWpU0eOosRlvOlI-MAOa

代码为:

#include<cstdio>
#include<cstring>
const int maxn=10+10;
char str[maxn];

int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}

int cal1(int x)
{
    int ans=1;
    for(int i=1;i<=x;i++)
        ans=ans*10;
    return ans;
}

int cal2(int x)
{
    int ans=0;
    for(int i=1;i<=x;i++)
        ans=ans*10+9;
    return ans;
}

int main()
{
    int t,ok,len,start,end,num;
    int son,mother,extra;
    int ans;
    scanf("%d",&t);
    while(t--)
    {
        ok=1;
        scanf("%s",str);
        len=strlen(str);
        for(int i=0;i<len;i++)
        {
            if(str[i]=='(')
               {
                   start=i;
                   ok=0;
               }
            if(str[i]==')')
               {
                   end=i;
                   ok=0;
               }
        }
        if(ok==1)
        {
            son=0;
            mother=cal1((len-2));
            for(int i=2;i<len;i++)
                son=son*10+(str[i]-'0');
        //    printf("mother:%lld son:%lld\n",mother,son);
            ans=gcd(son,mother);
            printf("%d/%d\n",son/ans,mother/ans);
        }
        else if(!ok&&start==2)
        {
            son=0;
            int circle=end-1-start;
            for(int i=3;i<end;i++)
                son=son*10+(str[i]-'0');
            mother=cal2(circle);
            ans=gcd(son,mother);
            printf("%d/%d\n",son/ans,mother/ans);
        }
        else
        {
            son=0;
            extra=0;
            int circle=end-1-start;
            for(int i=2;i<start;i++)
                extra=extra*10+str[i]-'0';
            for(int i=start+1;i<end;i++)
                son=son*10+(str[i]-'0');
            mother=cal2(circle);
            int copymother=cal1(start-2);
            //printf("copymother%I64d\n",copymother);
           // printf("extra:%lld son:%lld mother:%lld\n",extra,son,mother);
            son=mother*extra+son;
            ans=gcd(son,mother);
            son=son/ans;
            mother=mother/ans;
           // printf("%lld/%lld\n",son/ans,mother/ans);
            mother=mother*copymother;
            ans=gcd(son,mother);
            printf("%d/%d\n",son/ans,mother/ans);
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值