Fraction to Recurring Decimal|leetcode题解

这个题目由于有很多的特殊状况,所以错误提交了很多次。

下面是AC代码,耗时4ms

string fractionToDecimal(int numerator, int denominator) {
      //首先对0进行判断,以防对最终符号的异或运算结果有干扰    
       if(0==numerator)return "0";
        if(0==denominator)return "";
      //使用异或的方式,判断两个数相除的符号,非常好用
       bool sign=(numerator<0)^(denominator<0)?true:false;
        //防止溢出,使用long long
        long long lnumerator=llabs(numerator),ldenominator=llabs(denominator);
        long long integer=lnumerator/ldenominator;
    
        stringstream ss; 
        ss<<integer;
        string left=ss.str();
        left=(sign?"-":"")+left;
        ss.str("");ss.clear();
        if(0==lnumerator%ldenominator)return left; 
        lnumerator%=ldenominator;
       
        map<long long,int>loopPos;    
        string right="";
        bool loop=false;
        int index=0;
        int nZero=0;
        while(lnumerator){
            int i=1;
            while((long long)lnumerator*pow(10,i)<ldenominator)i++;
            long long t=lnumerator*pow(10,i);
            if(loopPos.count(t)){
                loop=true;
                index=loopPos[t];
                nZero=i-1;
                break;
            }
            index+=i;
            loopPos.insert(pair<long long,int>(t,index));
            lnumerator=t%ldenominator;
            long long quotient=t/ldenominator;
            for(int j=1;j<=i-1;j++)ss<<0;
            ss<<quotient;
        }   
        right=loop?ss.str().substr(0,index-1-nZero)+"("+ss.str().substr(index-1-nZero)+")":ss.str();
    
        return left+"."+right;
    }

此问题的基本思路,是确定循环小数循环开始和结束的地方,因此要使用散列表记录曾经出现的被除数,如果重复出现,则认为是循环小数。

循环小数结束的地方就是此时商所在的下标的前一位置。

但是循环小数开始的地方,并非是循环小数商首次出现的位置,因为这个商之前可能出现多个前导0,个数用nZero表示。

另外,此题运算过程中,不要使用int,我的编译器由于int和long所占位数相同,因此,为了避免越界,只能使用long long类型。

如果使用int类型,则可能出现INT_MIN的越界,并且此时abs(INT_MIN)依然是INT_MIN,无法在正数域进行计算。

另外,如果不打算在正数域进行计算,则余数的处理非常麻烦,因为在c++中,取余运算得到的余数的符号和被除数符号一致,

并且有可能和其他程序的规则不同,因此需要对被除数的符号进行判断,这样写较为麻烦,建议转到正数域运算。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android resource fraction refers to the ability to specify resource values as fractions or percentages of a parent resource. This is commonly used in Android development to create responsive layouts that adapt to different screen sizes and resolutions. For example, instead of specifying a fixed width or height for a view, you can use resource fractions to define the size relative to the parent container. This allows the view to scale proportionally when the container's size changes. To use resource fractions in Android, you can define them in XML resource files using the "fraction" unit. For instance, you can set the width of a view to half of the parent container by using a fraction value of "0.5". Here's an example of how you can use resource fractions in an XML layout file: ```xml <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <View android:layout_width="@fraction/0.5" android:layout_height="0dp" android:layout_weight="1" android:background="@color/red" /> <View android:layout_width="@fraction/0.5" android:layout_height="0dp" android:layout_weight="1" android:background="@color/blue" /> </LinearLayout> ``` In this example, two views are placed inside a LinearLayout with a vertical orientation. Both views have a width defined as half of the parent container, using the "@fraction/0.5" resource value. Using resource fractions can help create more flexible and responsive UI designs in Android applications.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值