leetcode 日经贴,Cpp code -Fraction to Recurring Decimal
来源:互联网 发布:2017十月经济数据公布 编辑:程序博客网 时间:2024/06/07 07:21
Fraction to Recurring Decimal
class Solution {public: string itostring(long long n) { string s; if (!n) return "0"; while (n) { s = char(n%10 + '0') + s; n /= 10; } return s; } string fractionToDecimal(int numerator, int denominator) { long long a = numerator, b = denominator; if(b == 0) return "error"; bool neg = (a < 0 && b > 0) || (a > 0 && b < 0); string ans = neg? "-":""; if (a < 0) a = -a; if (b < 0) b = -b; ans += itostring(a / b); a %= b; if (a == 0) return ans; vector<int> decimals; map<int, int> mp; ans += "."; while (a) { if (mp.find(a) != mp.end()) break; mp[a] = decimals.size(); a *= 10; decimals.push_back(a / b); a %= b; } for (int i = 0; i < decimals.size(); ++i) { if (a && mp[a] == i) { ans += '('; } ans += char('0' + decimals[i]); } if (a) { ans += ')'; } return ans; }};
0 0
- leetcode 日经贴,Cpp code -Fraction to Recurring Decimal
- [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
- 虚函数,抽象类,抽象函数,接口
- 第二章 17
- 智慧城市建设思考
- Elasticsearch之Nested Sorting
- Android Studio 的安装和配置篇(Windows篇)
- leetcode 日经贴,Cpp code -Fraction to Recurring Decimal
- 参考资源_
- Android点击效果加边框
- 第五届在线编程大赛月赛第三题:石子游戏(1)
- 使用pdshell15教程
- Oracle Cluster Health Monitor(CHM)
- Android中RelativeLayout各个属性 android:layout_alignParentLeft=”true”找不到有时候
- 浅说《测试用例》----给测试新手的
- sql行列转置的实现方法