分数转小数,要求输出循环小数 如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. (转载)selenium-webdriver(python)

    转载地址: http://www.cnblogs.com/fnng/p/3183777.html 本节重点: 简单对象的定位 -----自动化测试的核心 对象的定位应该是自动化测试的核心,要想操作一个 ...

  2. RHEL 集群(RHCS)配置小记 -- 文档记录

    1.RHEL 6 集群配置官方管理手册 https://access.redhat.com/site/documentation/zh-CN/Red_Hat_Enterprise_Linux/6/pd ...

  3. Samba服务器搭建配置

    本次环境: 服务端-centos6.8-192.168.2.200 客户端1-centos6.8-192.168.2.201 客户端2-Windows7-192.168.2.104 假设条件如下: 使 ...

  4. sqlserver中,查看某个函数的调用情况

    今天想在sqlserver中看看自己写的函数都被哪个函数或存储过程调用了,手工检查起来太慢了,于是在网上找一个快速的方法,分享一下. select * from sys.all_sql_modules ...

  5. Sql 中text类型字段判断是否为空

    用 len关键字,字段=''会报错:数据类型 text 和 varchar 在 equal to 运算符中不兼容. 正确方法: 1. 字段 is null 2. datalength(字段)=0 注: ...

  6. 0729am空控制器

  7. boa移植

    1.交叉编译 2.复制文件 配置文件boa.conf 移动到/etc/boa/ 目录下 可执行文件boa移动到/usr/sbin/目录下 3.修改配置文件 4.将Linux系统上/etc/mime.t ...

  8. [MongoDB]Mongodb攻略

    -------------------------------------------------------------------------------------------- [基础] 1. ...

  9. 【Maven】Eclipse 使用Maven创建SpringMVC Web项目

    创建环境 系统:win 10 软件:eclipse,maven. 创建步骤 创建一个Maven Web项目,可以参照:[Maven]Eclipse 使用Maven创建Java Web项目 添加spri ...

  10. JS,数组小练习

    var arr = [4, 0, 7, 9, 0, 0, 2, 6, 0, 3, 1, 0];要求将数组中的0项去掉,将不为0的值存入一个新的数组,生成新的数组 p.p1 { margin: 0.0p ...