分数转小数,要求输出循环小数 如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. datagridview中使用checkbox问题。

    如果套用datagridview中的checkboxfield,生成的数据,会出现无法选择datagridview中数据项的问题,即checkbox不可以被鼠标点击,选中/取消选中.此checkbox ...

  2. java基础之:堆排序

    最近做题目饱受打击,愈发觉得打好基础的重要性,于是乎,决心把基本的排序算法还有数组操作一一实现,目的在于一方面能够得到对JAVA基础的巩固,另一面在实现的过程中发现不足. 今天所实现的堆排序(最大堆) ...

  3. iterator 及 迭代器模式(转发)

    Iterator definitions An iterator is any object that, pointing to some element in a range of elements ...

  4. python datatime

    一.datetime 1.date date.today() 2.time 3.datetime datetime.now() datetime.strftime(fmt) 转换为字符串 dateti ...

  5. 使用commons-logging和log4j记录日志

    一,为什么要使用commons-logging+log4j? commons-logging和log4j都是Apache下的开源项目.commons-logging的目的是为“所有的Java日志实现” ...

  6. Verilog之SOS信号-仿顺序操作

    SOS信号:. . . _ _ _ . . . 1. module sos_module ( CLK, RSTn, Pin_Out, SOS_En_Sig ); input CLK; input RS ...

  7. C# HttpWebRequest与HttpWebResponse详解

    C# HttpWebRequest与HttpWebResponse详解  http://www.codeproject.com/Articles/6554/How-to-use-HttpWebRequ ...

  8. marquee标签属性详解(跑马灯文字效果)

    请大家先看下面这段代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...

  9. IOS学习笔记之关键词@dynamic

    IOS学习笔记之关键词@dynamic @dynamic这个关键词,通常是用不到的. 它与@synthesize的区别在于: 使用@synthesize编译器会确实的产生getter和setter方法 ...

  10. 【转】移动端viewport的使用

    web端网站转移至移动端页面,注意点如下: 1.首先引入viewport调整页面宽度 <meta name="viewport" content="width=de ...