C++實(shí)現(xiàn)LeetCode(166.分?jǐn)?shù)轉(zhuǎn)循環(huán)小數(shù))
[LeetCode] 166.Fraction to Recurring Decimal 分?jǐn)?shù)轉(zhuǎn)循環(huán)小數(shù)
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)".
Credits:
Special thanks to @Shangrila for adding this problem and creating all test cases.
這道題還是比較有意思的,開始還擔(dān)心萬一結(jié)果是無限不循環(huán)小數(shù)怎么辦,百度之后才發(fā)現(xiàn)原來可以寫成分?jǐn)?shù)的都是有理數(shù),而有理數(shù)要么是有限的,要么是無限循環(huán)小數(shù),無限不循環(huán)的叫無理數(shù),例如圓周率pi或自然數(shù)e等,小學(xué)數(shù)學(xué)沒學(xué)好,汗!由于還存在正負(fù)情況,處理方式是按正數(shù)處理,符號(hào)最后在判斷,那么我們需要把除數(shù)和被除數(shù)取絕對(duì)值,那么問題就來了:由于整型數(shù)INT的取值范圍是-2147483648~2147483647,而對(duì)-2147483648取絕對(duì)值就會(huì)超出范圍,所以我們需要先轉(zhuǎn)為long long型再取絕對(duì)值。那么怎么樣找循環(huán)呢,肯定是再得到一個(gè)數(shù)字后要看看之前有沒有出現(xiàn)這個(gè)數(shù)。為了節(jié)省搜索時(shí)間,我們采用哈希表來存數(shù)每個(gè)小數(shù)位上的數(shù)字。還有一個(gè)小技巧,由于我們要算出小數(shù)每一位,采取的方法是每次把余數(shù)乘10,再除以除數(shù),得到的商即為小數(shù)的下一位數(shù)字。等到新算出來的數(shù)字在之前出現(xiàn)過,則在循環(huán)開始出加左括號(hào),結(jié)束處加右括號(hào)。代碼如下:
class Solution { public: string fractionToDecimal(int numerator, int denominator) { int s1 = numerator >= 0 ? 1 : -1; int s2 = denominator >= 0 ? 1 : -1; long long num = abs( (long long)numerator ); long long den = abs( (long long)denominator ); long long out = num / den; long long rem = num % den; unordered_map<long long, int> m; string res = to_string(out); if (s1 * s2 == -1 && (out > 0 || rem > 0)) res = "-" + res; if (rem == 0) return res; res += "."; string s = ""; int pos = 0; while (rem != 0) { if (m.find(rem) != m.end()) { s.insert(m[rem], "("); s += ")"; return res + s; } m[rem] = pos; s += to_string((rem * 10) / den); rem = (rem * 10) % den; ++pos; } return res + s; } };
- C++實(shí)現(xiàn)LeetCode(173.二叉搜索樹迭代器)
- C++實(shí)現(xiàn)LeetCode(172.求階乘末尾零的個(gè)數(shù))
- C++實(shí)現(xiàn)LeetCode(170.兩數(shù)之和之三 - 數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì))
- C++實(shí)現(xiàn)LeetCode(169.求大多數(shù))
- C++實(shí)現(xiàn)LeetCode(171.求Excel表列序號(hào))
- C++實(shí)現(xiàn)LeetCode(168.求Excel表列名稱)
- C++實(shí)現(xiàn)LeetCode(167.兩數(shù)之和之二 - 輸入數(shù)組有序)
- C++實(shí)現(xiàn)LeetCode(179.最大組合數(shù))
相關(guān)文章
基于C++的攝像頭圖像采集及拼接程序的簡單實(shí)現(xiàn)
本程序是在?ubuntu14.04?平臺(tái)下實(shí)現(xiàn)的,在本項(xiàng)目目錄下,已經(jīng)有編譯生成的可執(zhí)行程序,其中Camera_to_Frmae.cpp是我們從雙攝像頭實(shí)時(shí)抓取單幀圖像的源碼,對(duì)基于C++的攝像頭圖像采集及拼接程序的實(shí)現(xiàn)感興趣的朋友一起看看吧2022-01-01Qt基礎(chǔ)開發(fā)之Qt文件操作類QFile讀寫文件的詳細(xì)方法與實(shí)例及QDataStream的使用方法
這篇文章主要介紹了Qt基礎(chǔ)開發(fā)之Qt文件操作類QFile讀寫文件的詳細(xì)方法與實(shí)例,需要的朋友可以參考下2020-03-03C++實(shí)現(xiàn)LeetCode(25.每k個(gè)一組翻轉(zhuǎn)鏈表)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(25.每k個(gè)一組翻轉(zhuǎn)鏈表),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C++實(shí)現(xiàn)LeetCode(150.計(jì)算逆波蘭表達(dá)式)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(150.計(jì)算逆波蘭表達(dá)式),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07C++?Boost?CircularBuffer算法超詳細(xì)精講
Boost是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱。Boost庫是一個(gè)可移植、提供源代碼的C++庫,作為標(biāo)準(zhǔn)庫的后備,是C++標(biāo)準(zhǔn)化進(jìn)程的開發(fā)引擎之一,是為C++語言標(biāo)準(zhǔn)庫提供擴(kuò)展的一些C++程序庫的總稱2022-11-11C語言創(chuàng)建數(shù)組實(shí)現(xiàn)函數(shù)init,empty,reverse
這篇文章主要介紹了C語言創(chuàng)建數(shù)組實(shí)現(xiàn)函數(shù)init,empty,reverse,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07