[leetcode]Fraction to Recurring Decimal
来源:互联网 发布:淘宝吉他店铺介绍 编辑:程序博客网 时间:2024/05/01 13:54
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.
整数部分和小数部分分开来做。用long long是因为分子分母取INT_MIN时取绝对值int型会溢出
class Solution {public: string fractionToDecimal(int numerator, int denominator) { if(numerator==0) return "0"; string ans= ""; if(numerator>0 ^ denominator>0) ans+='-'; long long num=(long long)numerator; long long den=(long long)denominator; num = abs(num); den = abs(den); long long res=num/den; long long rem=num%den; if(rem==0) return (ans+to_string(res)); else{ map<long long,int> m; ans += to_string(res) + '.'; string str = ""; for(int pos =0;rem!=0;pos++){ if(m.find(rem)!=m.end()){ str.insert(m[rem],"("); str +=')'; return ans+str; } m[rem]=pos; str += to_string(rem*10/den); rem = rem*10%den; } return ans+str; } }};
0 0
- [leetcode] Fraction to Recurring Decimal
- LeetCode Fraction to Recurring Decimal
- Fraction to Recurring Decimal leetcode
- leetcode:Fraction to Recurring Decimal
- [LeetCode] Fraction to Recurring Decimal
- [leetCode] Fraction to Recurring Decimal
- LeetCode:Fraction to Recurring Decimal
- Leetcode: Fraction to Recurring Decimal
- Leetcode: Fraction to Recurring Decimal
- [LeetCode] Fraction to Recurring Decimal
- [LeetCode]Fraction to Recurring Decimal
- leetcode:Fraction to Recurring Decimal
- [LeetCode]Fraction to Recurring Decimal
- Leetcode: Fraction to Recurring Decimal
- [leetcode]Fraction to Recurring Decimal
- LeetCode: Fraction to Recurring Decimal
- [LeetCode] Fraction to Recurring Decimal
- leetcode: Fraction to Recurring Decimal
- 一道匿名内部类的测试题
- 12306 图形验证码闲谈
- LeetCode 62.Unique Paths
- 【项目3 - 时间类】阅读、运行程序后,按要求扩充类的功能 第2周项目
- 再被绝杀
- [leetcode]Fraction to Recurring Decimal
- android自带的媒体数据库应用
- Dynea phenolic film coated plywood the concrete formwork shuttering panel
- Bag标签之校验
- Android Studio编译时,adb报错的解决方法
- C编译器剖析_4.4 语义检查_外部声明_类型结构的构建(1)
- 【bzoj 2666】: [cqoi2012]组装
- 关于P2P和VoIP的相关资料
- c++面试题