Fraction to Recurring Decimal(STRING-TYPE CONVERTION)

QUESTION

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, return "0.5".
Given numerator = 2, denominator = 1, return "2".
Given numerator = 2, denominator = 3, return "0.(6)".

1ST TRY

class Solution {

public:

    string fractionToDecimal(int numerator, int denominator) {

        char* ch  = new char();

        string ret;

        string fractionStr = "";

        int remain;

        vector<bool> flag(10, false);

        

        //integer part

        *ch = numerator/denominator +'0';

        ret = ch;

        

        //fraction part

        while(1)

        {

            remain = numerator%denominator;

            if(remain == 0) return ret;

            else if(flag[remain]) return ret + ".(" + fractionStr + ")";

            else

            {

                flag[remain] = true;

                *ch = numerator/denominator +'0';

                fractionStr += ch;

            }

        }

    }

};

Result: Runtime Error

Last executed input: -50, 8

2ND TRY

考虑了负数情况

class Solution {

public:

    string fractionToDecimal(int numerator, int denominator) {

        string intStr = "";

        string fractionStr = "";

        string tmpStr = "";

        int remain;

        vector<bool> flag(10, false);

        

        // int 转 string

        stringstream ss;

    

        //negative or not

        if(numerator > 0 && denominator < 0)

        {

            intStr = "-";

            denominator = ~(denominator-1);

        }

        else if(numerator < 0 && denominator > 0) 

        {

            intStr = "-";

            numerator = ~(numerator-1);

        }

        else if(numerator < 0 && denominator < 0) 

        {

            numerator = ~(numerator-1);

            denominator = ~(denominator-1);

        }

        

        //integer part

        ss << numerator/denominator;

        ss >> tmpStr;

        intStr += tmpStr;

        remain = numerator%denominator;

        if(remain == 0) return intStr;

        

        //fraction part

        while(1)

        {

            if(remain == 0) return intStr + "." + fractionStr;

            else if(flag[remain]) return intStr + ".(" + fractionStr + ")";

            else

            {

                flag[remain] = true;

                numerator = remain * 10;

                remain = numerator%denominator;

                ss.clear();

                ss << numerator/denominator;

                ss >> tmpStr;

                fractionStr += tmpStr;

            }

        }

    }

};

Result: Runtime Error

Last executed input: -2147483648, -10

3RD TRY

考虑溢出的情况

class Solution {

public:

    string fractionToDecimal(int numerator, int denominator) {

        long long int num = (long long int) numerator;

        long long int den = (long long int) denominator;

        

        string intStr = "";

        string fractionStr = "";

        string tmpStr = "";

        int remain;

        set<int> flag;

        

        // int 转 string

        stringstream ss;

    

        //negative or not

        if(num > 0 && denominator < 0)

        {

            intStr = "-";

            den = ~(den-1);

        }

        else if(num < 0 && den > 0) 

        {

            intStr = "-";

            num = ~(num-1);

        }

        else if(num < 0 && den < 0) 

        {

            num = ~(num-1);

            den = ~(den-1);

        }

        

        //integer part

        ss << num/den;

        ss >> tmpStr;

        intStr += tmpStr;

        remain = num%den;

        if(remain == 0) return intStr;

        

        //fraction part

        while(1)

        {

            if(remain == 0) return intStr + "." + fractionStr;

            else if(flag.find(remain)!=flag.end()) return intStr + ".(" + fractionStr + ")";

            else

            {

                flag.insert(remain);

                num = remain * 10;

                remain = num%den;

                ss.clear();

                ss << num/den;

                ss >> tmpStr;

                fractionStr += tmpStr;

            }

        }

    }

};

Result: Wrong

Input: 1, 6
Output: "0.(16)"
Expected: "0.1(6)"

4TH TRY

循环的位置得准确,所以用map代替set

class Solution {

public:

    string fractionToDecimal(int numerator, int denominator) {

        long long int num = (long long int) numerator;

        long long int den = (long long int) denominator;

        

        string ret = "";

        string tmpStr = "";

        int remain;

        map<int,int> flag;

        

        // int 转 string

        stringstream ss;

    

        //negative or not

        if(num > 0 && denominator < 0)

        {

            ret = "-";

            den = ~(den-1);

        }

        else if(num < 0 && den > 0) 

        {

            ret = "-";

            num = ~(num-1);

        }

        else if(num < 0 && den < 0) 

        {

            num = ~(num-1);

            den = ~(den-1);

        }

        

        //integer part

        ss << num/den;

        ss >> tmpStr;

        ret += tmpStr;

        remain = num%den;

        if(remain == 0) return ret;

        

        //fraction part

        ret += ".";

        while(1)

        {

            if(remain == 0) return ret;

            else if(flag.find(remain)!=flag.end())

            {

                ret.insert(flag[remain], 1, '('); 

                return ret + ")";

            }

            else

            {

                flag[remain] = ret.length();

                num = remain * 10;

                remain = num%den;

                ss.clear();

                ss << num/den;

                ss >> tmpStr;

                ret += tmpStr;

            }

        }

    }

};

Result: Wrong

Input: -1, -2147483648
Output: "0.000000000000000000000000000000-1"
Expected: "0.0000000004656612873077392578125"

5TH TRY

remain也要申请为long long int, 否则在num = remain * 10;会溢出

class Solution {

public:

    string fractionToDecimal(int numerator, int denominator) {

        long long int num = (long long int) numerator;

        long long int den = (long long int) denominator;

        

        string ret = "";

        string tmpStr = "";

        long long int remain;

        map<int,int> flag;

        

        // int 转 string

        stringstream ss;

    

        //negative or not

        if(num > 0 && denominator < 0)

        {

            ret = "-";

            den = ~(den-1);

        }

        else if(num < 0 && den > 0) 

        {

            ret = "-";

            num = ~(num-1);

        }

        else if(num < 0 && den < 0) 

        {

            num = ~(num-1);

            den = ~(den-1);

        }

        

        //integer part

        ss << num/den;

        ss >> tmpStr;

        ret += tmpStr;

        remain = num%den;

        if(remain == 0) return ret;

        

        //fraction part

        ret += ".";

        while(1)

        {

            if(remain == 0) return ret;

            else if(flag.find(remain)!=flag.end())

            {

                ret.insert(flag[remain], 1, '('); 

                return ret + ")";

            }

            else

            {

                flag[remain] = ret.length();

                num = remain * 10;

                remain = num%den;

                ss.clear();

                ss << num/den;

                ss >> tmpStr;

                ret += tmpStr;

            }

        }

    }

};

Result: Accepted

 

你可能感兴趣的:(convert)