LeetCode 166. Fraction to Recurring Decimal(模拟)

题目

题意:给出一个分数的分子和分母,给出这个分数的小数形式的字符串模式。循环的部分用( 括上。

题解:模拟除法,判断循环体。

class Solution {
public:
    map<int,int> m;
    string fractionToDecimal(int numerator, int denominator) {
       long long int numerator_ = numerator;
       long long int denominator_ = denominator;
        int sign = 1;
        if(numerator_<0&&denominator_>0)
        {
            sign = 0;
        }

        else if(numerator_>0&&denominator_<0)
        {

            sign = 0;
        }

         if(numerator_<0)
            numerator_ *= -1;

        if(denominator_<0)
            denominator_ *= -1;

       string ans="";

       if(numerator_ > denominator_)
       {
           long long int x = numerator_ / denominator_;
           numerator_ = numerator_ % denominator_;
           ans += to_string(x);
       }
       else
       {
           ans += '0';
       }

         m[numerator_] = 1;

       if(numerator_!=0)
        ans+='.';

       int tag=1;
       int pos=0;

       while(numerator_!=0)
       {
           numerator_ *= 10;

           long long int  x = numerator_ / denominator_ ;

           ans += (x+'0');

           numerator_ = numerator_ % denominator_;  

           if(numerator_==0)
               break;

           if(m[numerator_]!=0)
           {
               pos = m[numerator_];
               break;
           }
           else
           {
               m[numerator_]=++tag;
           }
       }

        string res="";
        if(sign==0)
            res += '-';
        int i;
        tag=-1;
        int tag2=0;
        for(i=0;i<ans.length();i++)
        {

            if(tag==pos)
            {
                res+='(';
                tag2=1;
            }

            res+=ans[i];

            if(tag>=1)
            {
                tag++;
            }

            if(ans[i]=='.')
            {
                tag=1;
            }
        }

        if(tag2==1)
        res+=')';

        return res;

    }
};

原文地址:https://www.cnblogs.com/dacc123/p/12236047.html

时间: 2024-07-30 05:08:44

LeetCode 166. Fraction to Recurring Decimal(模拟)的相关文章

[leetcode] 166. Fraction to Recurring Decimal 解题报告

题目链接: https://leetcode.com/problems/fraction-to-recurring-decimal/ Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in p

Java for LeetCode 166 Fraction to Recurring Decimal

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. For example, Given numerator = 1, denominator = 2, retu

leetcode[166] Fraction to Recurring Decimal

For example, Given numerator = 1, denominator = 2, return "0.5". Given numerator = 2, denominator = 1, return "2". Given numerator = 2, denominator = 3, return "0.(6)". 按照上例子实现结果.主要就是如何处理循环的数字. 我们发现如果循环了,那么余数也是出现了循环,所以从余数入手,一

【LeetCode】Fraction to Recurring Decimal

Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. For example, Given numera

[LeetCode#116]Fraction to Recurring Decimal

Problem: Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. For example, Given numerator = 1, denominator

【LeetCode】Fraction to Recurring Decimal【Solution】

[题目] Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. For example, Given numerator = 1, denominator = 2,

166. Fraction to Recurring Decimal

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. For example, Given numerator = 1, denominator = 2, retu

166. Fraction to Recurring Decimal -- 将除法的商表示成字符串(循环节用括号表示)

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. For example, Given numerator = 1, denominator = 2, retu

[LeetCode]58. Fraction to Recurring Decimal分数化小数

Given two integers representing the numerator and denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses. For example, Given numerator = 1, denominator = 2, retu