Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环
分数转小数,要求输出循环小数 如2 3 输出0.(6)
弗洛伊德判环的原理是在一个圈里,如果一个人的速度是另一个人的两倍,那个人就能追上另一个人。代码中one就是速度1的人,而two就是速度为2的人。
Fraction to Recurring Decimal可以使用弗洛伊德判环,不同的是要找到循环出现的起始点,因此会比单单判断是否循环出现的题要难一点,代码要长一点,但是它比是用map的实现会更快。
要做出这道题有几个注意点:
1)对于整数的求余和整除运算要注意,特别负数的求余运算, 可以参考 getMode_()和getShang_();
2) 由于整数整除的特殊性,对于整数整除是0的无法判断正负,因此要转化成double再判断;
3)对于弗洛伊德判环请注意退出条件和找到循环出现的起始点和结束点,注意初始化。
class Solution {
public:
typedef long long __int64;
inline __int64 getShang_(__int64 numerator, __int64 denominator){//特别注意数字都要取绝对值
return abs(numerator) / abs(denominator);
}
inline __int64 getMod_(__int64 numerator, __int64 denominator){//特别注意数字都要取绝对值
return abs(numerator) % abs(denominator);
}
inline std::string inttostr_(__int64 num){
char t[] = { };
sprintf(t, "%lld", num);
return std::string(t);
}
std::string fractionToDecimal(int numerator, int denominator) {
std::string Integer_("");
if ((double)numerator / denominator < ) Integer_ = "-"; //整数整除是0的无法判断正负
Integer_ += inttostr_(getShang_(numerator, denominator));
if ( == getMod_(numerator,denominator) ) {
return Integer_;
}
Integer_ += ".";
__int64 tnumerator = numerator;
tnumerator = getMod_ (tnumerator, denominator);
//printf("%lld\n", numerator);
__int64 one = tnumerator;
__int64 two = tnumerator;
std::vector<__int64> vyushu;
std::string Decimal_("");
vyushu.push_back(two);
int cnt = ; //循环节周期
while (true)
{
one = getMod_(one * , denominator);
Decimal_ += inttostr_(getShang_(two * , denominator));
two = getMod_(two * , denominator);
vyushu.push_back(two);
if ( == two) break;//退出条件
Decimal_ += inttostr_(getShang_(two * , denominator));
two = getMod_(two * , denominator);
vyushu.push_back(two);
if ( == two) break;//退出条件
cnt++;
if (two == one) break;
}
/*for (auto i:vyushu){
std::cout << i << std::endl;
}
std::cout << Decimal_ << std::endl;
std::cout << cnt << std::endl;*/
if (two){ //找到循环出现的起始点和结束点[j,k)
int j = , k = ;
for (std::vector<int>::size_type i = vyushu.size()-; i >= && i>=cnt; --i){
if (vyushu[ i -cnt] != vyushu[ i]){
j = i - cnt + ;
break;
}
}
for (std::vector<int>::size_type i = j + ; i < vyushu.size(); ++i){
if (vyushu[i] == vyushu[j]) {
k = i;
break;
}
}
//std::cout << j << " " << k << std::endl;
return Integer_ + Decimal_.substr(, j) + "(" + Decimal_.substr(j, k - j) +")";
}
else return Integer_ + Decimal_;
}
};
在代码中注释部分出现的某些代码是来自c++11标准的,请忽略! 另外c++11标准直接支持__int64。
Leetcode 166. Fraction to Recurring Decimal 弗洛伊德判环的更多相关文章
- ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Java for LeetCode 166 Fraction to Recurring Decimal
Given two integers representing the numerator and denominator of a fraction, return the fraction in ...
- Leetcode#166 Fraction to Recurring Decimal
原题地址 计算循环小数 先把负数转化成正数,然后计算,最后添加符号 当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数 需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧 ...
- 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)
[LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...
- 【LeetCode】166. Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 【刷题-LeetCode】166 Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 【leetcode】Fraction to Recurring Decimal
Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...
- 166. Fraction to Recurring Decimal
题目: Given two integers representing the numerator and denominator of a fraction, return the fraction ...
- [LeetCode#116]Fraction to Recurring Decimal
Problem: Given two integers representing the numerator and denominator of a fraction, return the fra ...
随机推荐
- java基础之:堆排序
最近做题目饱受打击,愈发觉得打好基础的重要性,于是乎,决心把基本的排序算法还有数组操作一一实现,目的在于一方面能够得到对JAVA基础的巩固,另一面在实现的过程中发现不足. 今天所实现的堆排序(最大堆) ...
- jsp学习--基本语法和基础知识
一.JSP简单介绍 1.什么是JSP? JSP全称是Java Server Pages,它和servle技术一样,都是SUN公司定义的一种用于开发动态web资源的技术. JSP这门技术的最大的特点在于 ...
- 文件上传之Html5 + jQuery上传、asp.net web api接收
HTML: <div> <label for="fileUpload"> 选择文件 </label> <br/> <input ...
- asp.net用url重写URLReWriter实现任意二级域名
本文转自 http://www.cnblogs.com/notus/archive/2007/03/13/673222.html
- Java中XML格式的字符串4读取方式的简单比较
Java中XML格式的字符串4读取方式的简单比较 1.java自带的DOM解析. import java.io.StringReader; import javax.xml.parsers.Docum ...
- php折半查找(数组必须为有序)
$arr=array('1','7','9','11','20','23','33','44','50'); $len=count($arr); $low=0;$high=$len- ...
- hibernate报错及解决方式
1 java.sql.BatchUpdateException: ORA-01438: 值大于为此列指定的允许精度 原因:pl/sql number(n),插入的位数大于n
- table中的标题行冻结的简单实现
这里只是简单的实现,主要是用了position属性的fixed属性值,这个属性值需要高版本浏览器的支持,如果要兼容低版本的浏览器可以通过写脚本的方式实现,也可以使用UI库,有些UI库里面表格插件的标题 ...
- ASP连接数据库登录按钮
(1)在配置文件中加上以下代码: <connectionStrings> <add name="accessconn" connectionString=&quo ...
- 【排列组合】bzoj3505 [Cqoi2014]数三角形
http://blog.csdn.net/zhb1997/article/details/38474795 #include<cstdio> #include<algorithm&g ...