leetcode166. 分数到小数

class Solution {
public:
    string fractionToDecimal(int numerator, int denominator) {
        string ans="";
        long x=numerator;
        long y=denominator;
        if(x % y == 0){
            return to_string(x/y);
        }
        if(x<0 ^ y<0)ans += "-";//正负
        x=abs(x);
        y=abs(y);
        ans += to_string(x/y) + ".";//整数部分
        //小数
        string f="";
        long z= x % y;
        int index=0;
        unordered_map<long,int> mp;
        while(z != 0&&!mp.count(z)){
            mp[z] = index;
            z *= 10;
            f += to_string(z/y);
            z %= y;
            index++;
        }
        //循环
        if(z!=0){
            int insertIndex = mp[z];
            f = f.substr(0,insertIndex)+"("+f.substr(insertIndex);
            f +=")";
        }
        ans += f;
        return ans;
    }
};