分数转小数,要求输出循环小数 如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 弗洛伊德判环的更多相关文章

  1. ✡ leetcode 166. Fraction to Recurring Decimal 分数转换 --------- java

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  2. Java for LeetCode 166 Fraction to Recurring Decimal

    Given two integers representing the numerator and denominator of a fraction, return the fraction in ...

  3. Leetcode#166 Fraction to Recurring Decimal

    原题地址 计算循环小数 先把负数转化成正数,然后计算,最后添加符号 当被除数重复出现的时候,说明开始循环了,所以用一个map保存所有遇到的被除数 需要考虑溢出问题,这也是本题最恶心的地方,看看通过率吧 ...

  4. 【LeetCode】166. Fraction to Recurring Decimal 解题报告(Python)

    [LeetCode]166. Fraction to Recurring Decimal 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingz ...

  5. 【LeetCode】166. Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

  6. 【刷题-LeetCode】166 Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

  7. 【leetcode】Fraction to Recurring Decimal

    Fraction to Recurring Decimal Given two integers representing the numerator and denominator of a fra ...

  8. 166. Fraction to Recurring Decimal

    题目: Given two integers representing the numerator and denominator of a fraction, return the fraction ...

  9. [LeetCode#116]Fraction to Recurring Decimal

    Problem: Given two integers representing the numerator and denominator of a fraction, return the fra ...

随机推荐

  1. Sorted Union

    function unite(arr1, arr2, arr3) { //return arr1; var args = Array.from(arguments); var arr = args.r ...

  2. expdp小记

    一.10.2.0.5要求expdp导出a用户b表空间下的数据. expdp \'/ as sysdba\' directory=mydir dumpfile=1.dmp schemas=a exclu ...

  3. Lua.LearningLua.7-userdata

    Learning Lua: 7 - userdata 1. Userdata basic "There are eight basic types in Lua: nil, boolean, ...

  4. 学习Linux所需软件

    一:  VMware虚拟机 二:VMware_Install_Cleaner 虚拟机卸载 三:XShall

  5. 你必须了解的基础的 Linux 网络命令

    Linux 基础网络命令列表 我在计算机网络课程上使用 FreeBSD,不过这些 UNIX 命令应该也能在 Linux 上同样工作. 连通性 ping <host>:发送 ICMP ech ...

  6. Sql Server 查询第30条数据到第40条记录数

    1.select top  10 *  from  (select top  40 * from tablename order by id desc);

  7. [Chapter 3 Process]Practice 3.4 Describe what happens when a context switch occurs if the new context is already loaded into one of the register sets.

    3.4 The Sun UltraSPARC processor has multiple register sets. Describe what happens when a context sw ...

  8. Map:containsKey、containsValue 获取Map集合的键值的 值

    get(Object key) 返回与指定键关联的值: containsKey(Object key) 如果Map包含指定键的隐射,则返回true: containsValue(Object valu ...

  9. SpringMVC中使用DWR

    SpringMVC中使用DWR重点在其配置当中. 1.  web.xml文件的配置 在DispatcherServlet中增加dwr的拦截来取代DwrServlet. 更改配置如下: <serv ...

  10. linux环境变量查看及修改

    例如用命令 echo $PATH 则可以查看该环境变量为/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin 添加环境定义一个变量 ...