[LeetCode] Fraction to Recurring Decimal

简介: 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 parenthes

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)".
解题思路:
首先得到商的整数部分,余数部分不能整除则在余数后面加0。用一个vector保存每次相除后的余数,出现了相同的余数,则意味着有循环小数。
一个容易忽略的点是INT_MIN,即0x80000000,它的整数用int保存不了,需要转换格式为long long。
实现代码:
/*****************************************************************************
    *  @COPYRIGHT NOTICE
    *  @Copyright (c) 2015, 楚兴
    *  @All rights reserved
    *  @Version  : 1.0

    *  @Author   : 楚兴
    *  @Date     : 2015/2/6 19:57
    *  @Status   : Accepted
    *  @Runtime  : 18 ms
*****************************************************************************/
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
class Solution {
public:
	string fractionToDecimal(int numerator, int denominator) {
		if (denominator == 0)
		{
			return ""; //分母为0
		}
		if (numerator == 0)
		{
			return "0"; //分子为0
		}

		long long num = numerator;
		long long den = denominator;

		bool flag = false;
		if ((num > 0 && den < 0) ||(num < 0 && den > 0))
		{
			flag = true;
		}
		num = num >= 0 ? num : -num;
		den = den > 0 ? den : - den;

		string str = "";
		long long result = num / den; //商的整数部分
		char res[11];
		itoa(result,res);
		int len = strlen(res);
		str += res;

		long long n = num % den;
		if (!n)
		{
			if (flag)
			{
				str.insert(str.begin(),'-');
			}
			return str;
		}
		str += '.';
		
		vector<long long> tail;
		int index = -1;
		while(n)
		{
			tail.push_back(n);
			n *= 10;
			str += n / den + '0';
			n %= den;
			index = find(tail,n);
			if (index != -1)
			{
				break;
			}
		}
		if (index != -1)
		{
			str.insert(str.begin() + index + len + 1, '(');
			str.push_back(')');
		}
		if (flag)
		{
			str.insert(str.begin(),'-');
		}
		
		return str;
	}

	int find(vector<long long> num, long long n)
	{
		for (int i = 0; i < num.size(); i++)
		{
			if (num[i] == n)
			{
				return i;
			}
		}
		return -1;
	}

	void itoa(long long n, char* ch) //n不为负
	{
		char* temp = ch;
		if (n == 0)
		{
			*temp++ = '0';
			*temp = '\0';
			return;
		}
		while(n)
		{
			*temp++ = n % 10 + '0';
			n /= 10;
		}
		*temp = '\0';
		reverse(ch);
	}

	void reverse(char* ch)
	{
		char* start = ch;
		char* end = ch + strlen(ch) - 1;
		char c;
		while(start < end)
		{
			c = *start;
			*start++ = *end;
			*end-- = c;
		}
	}
};

int main()
{
	Solution s;
	cout<<s.fractionToDecimal(-2147483648, 1).c_str()<<endl;
	cout<<s.fractionToDecimal(-1,-2147483648).c_str()<<endl;
	cout<<s.fractionToDecimal(1,6).c_str()<<endl;
	cout<<s.fractionToDecimal(-6,-7).c_str()<<endl;
	system("pause");
}

目录
相关文章
|
网络架构
LeetCode 166 Fraction to Recurring Decimal (从分数到循环小数)(*)
版权声明:转载请联系本人,感谢配合!本站地址:http://blog.csdn.net/nomasp https://blog.csdn.net/NoMasp/article/details/51726454 翻译 给定两个整数,用于表示一个分数的分子和分母,以字符串格式返回这个分数。
1099 0
[LeetCode] Fraction to Recurring Decimal
Well, the key to this problem is on how to identify the recurring parts. After doing some examples using pen and paper, you may find that for the decimal parts to recur, the remainders should recur.
839 0
|
5天前
|
Unix Shell Linux
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
本文提供了几个Linux shell脚本编程问题的解决方案,包括转置文件内容、统计词频、验证有效电话号码和提取文件的第十行,每个问题都给出了至少一种实现方法。
LeetCode刷题 Shell编程四则 | 194. 转置文件 192. 统计词频 193. 有效电话号码 195. 第十行
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 32 - III. 从上到下打印二叉树 III
本文介绍了两种Python实现方法,用于按照之字形顺序打印二叉树的层次遍历结果,实现了在奇数层正序、偶数层反序打印节点的功能。
44 6
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 26. 树的子结构
这篇文章提供了解决LeetCode上"剑指Offer 26. 树的子结构"问题的Python代码实现和解析,判断一棵树B是否是另一棵树A的子结构。
39 4
|
2月前
|
搜索推荐 索引 Python
【Leetcode刷题Python】牛客. 数组中未出现的最小正整数
本文介绍了牛客网题目"数组中未出现的最小正整数"的解法,提供了一种满足O(n)时间复杂度和O(1)空间复杂度要求的原地排序算法,并给出了Python实现代码。
82 2
|
5天前
|
数据采集 负载均衡 安全
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
本文提供了多个多线程编程问题的解决方案,包括设计有限阻塞队列、多线程网页爬虫、红绿灯路口等,每个问题都给出了至少一种实现方法,涵盖了互斥锁、条件变量、信号量等线程同步机制的使用。
LeetCode刷题 多线程编程九则 | 1188. 设计有限阻塞队列 1242. 多线程网页爬虫 1279. 红绿灯路口
|
2月前
|
索引 Python
【Leetcode刷题Python】从列表list中创建一颗二叉树
本文介绍了如何使用Python递归函数从列表中创建二叉树,其中每个节点的左右子节点索引分别是当前节点索引的2倍加1和2倍加2。
38 7
|
2月前
|
Python
【Leetcode刷题Python】剑指 Offer 30. 包含min函数的栈
本文提供了实现一个包含min函数的栈的Python代码,确保min、push和pop操作的时间复杂度为O(1)。
20 4