Fraction to Recurring Decimal
来源:互联网 发布:淘宝会所建筑设计 编辑:程序博客网 时间:2024/05/16 09:25
Given two integers representing the numerator and denominator of a fraction, return the fraction in string format.
If the fractional part is repeating, enclose the repeating part in parentheses.
For example,
- Given numerator = 1, denominator = 2, return "0.5".
- Given numerator = 2, denominator = 1, return "2".
- Given numerator = 2, denominator = 3, return "0.(6)".
题意: 给定两个整型数,一个代表分子 numerator ,一个代表分母 denominator,以小数的形式返回它们的结果result,当有循环小数时,以括号形式表示。比如5/3=1.666666...以“1.(6)”的形式返回,返回的类型是字符串。
解题要点:
1、注意两数的正负号,保证求得结果的符号正确性。
2、INT_MIN的处理,将 INT_MIN转化为正数会溢出,即-2147483648变成整数会溢出,因此要使用long long int来计算。
long long int A = abs(numerator), B = abs(denominator);若denominator = -2147483648,则B仍为-2147483648应该改为 long long int A = numerator, B = denominator; A = abs(A); B = abs(B); 或者 long long int A = abs((long long int)numerator), B = abs((long long int)denominator);
3、分为整数部分和小数部分,重点在于小数部分的处理,因为小数部分有可能会出现循环。观察下面的除法:
0.16 6 ) 1.00 0 1 0 <-- Remainder=1, mark 1 as seen at position=0. - 6 40 <-- Remainder=4, mark 4 as seen at position=1. - 36 4 <-- Remainder=4 was seen before at position=1, so the fractional part which is 16 starts repeating at position=1 => 1(6).
当余数为4时,将出现循环,所以我们可以设置一个哈希表,存储每一次的余数,以及该余数在返回结果result中的下标。每一次得到新的余数,就查询该余数是否已经在哈希表中,是的话说明开始循环了,那么直接在result中该余数对应的位置后面插入‘(’,result末尾加上‘)’,结束运算。如果在哈希表中没找到,则继续正常运运算。
C++代码:
#include <iostream> #include <unordered_map>#include <sstream>#include <string>#include <cmath>using namespace std; class Solution {public: string fractionToDecimal(int numerator, int denominator) { if(0 == numerator)return "0"; string result;if(numerator < 0 ^ denominator < 0)//判断正负号,异或只有一个为真result = result + "-";/**************************************************************long long int A = abs(numerator), B = abs(denominator);若denominator = -2147483648,则B仍为-2147483648应该改为 long long int A = numerator, B = denominator; A = abs(A); B = abs(B); 或者 long long int A = abs((long long int)numerator), B = abs((long long int)denominator);***************************************************************/long long int A = numerator, B = denominator;//防止将INT_MIN转化为正数会溢出A = abs(A);B = abs(B);unordered_map<long long int, long long int> object;//存放余数和标号long long int num1 = 0;//整数部分的位数long long int num2 = -1;//小数部分的位数long long int shang = 0;long long int yushu = 0;while(1){shang = A / B;yushu = A % B;stringstream ss; ss<<shang; result = result + ss.str();if(object.count(yushu) != 0)//该余数曾经出现过break;if(-1 == num2)num1 = result.size();//整数部分长度if(0 == yushu)break;else if(-1 == num2)result = result + ".";object.insert(make_pair(yushu, num1 + 2 + num2));//记录余数的位置num2++; A = yushu * 10; }if(yushu != 0)//若没有除整{unordered_map<long long int, long long int>::iterator it = object.find(yushu);//找到插入括号的位置result.insert(it->second, "(");result = result + ")";} return result; }};int main(int argc, char* argv[]) { Solution temp;temp.fractionToDecimal(-1, -2147483648); system("pause"); }Python代码:
#coding=gbkimport mathclass Solution: # @return a string def fractionToDecimal(self, numerator, denominator): if 0 == numerator: return "0" result = "" dic = {} if (numerator < 0) ^ (denominator < 0): result = result + "-" numerator = abs(numerator) denominator = abs(denominator) num1 = 0 num2 = -1 while(1): shang = numerator / denominator yushu = numerator % denominator result = result + str(shang) if 0 == yushu: break if yushu in dic: result = result[0:dic[yushu]] + "(" + result[dic[yushu]:] + ")" break if -1 == num2: num1 = len(result) result = result + '.' dic[yushu] = num1 + 2 + num2 num2 = num2 + 1 numerator = yushu * 10 return resultif __name__ == '__main__': object = Solution() result = object.fractionToDecimal(-50, 8) print result
0 0
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to recurring decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- Fraction to Recurring Decimal
- CentOS 6.3 64位下彻底卸载mysql5.5.25图解教程
- ASP.NET 实现发送邮件 + 多个收件人 + 多个附件
- hadoop :java.io.FileNotFoundException: File does not exist:
- Android开发_Activity组件
- discuss(同乐第一个项目)导入新项目运行步骤
- Fraction to Recurring Decimal
- 使用java访问WEB站点
- stm32下载方式之串口ISP\swd(JLink)
- Gc2235camera porting guide
- VS2010系统找不到指定的文件
- 12.18android绘图,橡皮擦
- UVA - 112 - Tree Summing (数的求和!栈的应用!)
- controller测试以及Date格式转换
- 回调 Objective-c 实现研究(未完成)