给定字符串N形式的浮点数,任务是将给定的浮点数转换为分数。
Enclosed sequence of digits in “()” in the floating-point representation expresses recurrence in the decimal representation.
For example, 1.(6) represents 1.666….
例子:
Input: N = “1.5”
Output: 3 / 2
Explanation:
The value of 3 / 2 will be equal to 1.5
Input: N = “1.(6)”
Output: 5 / 3
Explanation:
The value of 5 / 3 will be equal to 1.666… which is represented as 1.(6).
方法:想法是使用两个数的最大公约数和一些数学方程式来解决该问题。请按照以下步骤解决问题:
- 除重复序列外,让小数点后有x个数字。
- 如果没有重复序列,则将给定数字乘以10 x,并让GCD为10 x ,得到的数字为g 。打印结果除以g作为分子,并乘以10 x除以g作为分母。
For example, if N = “1.5” then x = 1.
Multiplying N with 10, the resultant will be 15 and the GCD of 10 and 15 is 3.
Therefore, print 15/3 = 5 as the numerator and 10/5 as the denominator.
- 如果存在重复序列,则将N乘以10 x 。例如,如果N = 23.98(231)乘以N *(10 2 ) 。
- 令序列中的总位数为y 。对于10 2 * N = 2398.(231) , y变为3 。
- 将10 y乘以N * 10 x 。对于10 2 * N = 2398.(231) ,将其乘以10 3,即10 2 * N * 10 3 = 2398231.(231) 。
- 现在,将N * 10 y + x与N * 10 x相减,结果为M。对于上面的示例, 10 2 * N *(10 3 -1)= 2395833 。
- 因此, N = M /((10 x )*(10 y – 1)) 。对于上面的示例, N = 2395833/999000 。
- 找出M和(((10 x )*(10 y – 1)))的GCD ,并打印M / gcd作为分子,并以((10 x )*(10 y – 1))作为分母。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to convert the floating
// values into fraction
void findFraction(string s)
{
// Initialize variables
string be_deci = "",
af_deci = "",
reccu = "";
bool x = true, y = false,
z = false;
// Traverse the floating string
for (int i = 0; i < s.size(); ++i) {
// Check if decimal part exist
if (s[i] == '.') {
x = false;
y = true;
continue;
}
// Check if recurrence
// sequence exist
if (s[i] == '(') {
z = true;
y = false;
continue;
}
// Retrive decimal part
// and recurrence resquence
if (x)
be_deci += s[i];
if (y)
af_deci += s[i];
if (z) {
// Traverse the string
for (; i < s.size()
&& s[i] != ')';
++i)
reccu += s[i];
break;
}
}
// Convert string to integer
int num_be_deci = stoi(be_deci);
int num_af_deci = 0;
// If no recurrence sequence exist
if (af_deci.size() != 0)
num_af_deci = stoi(af_deci);
// Initialize numerator & denominator
int numr = num_be_deci
* pow(10, af_deci.size())
+ num_af_deci;
int deno = pow(10, af_deci.size());
// No reccuring term
if (reccu.size() == 0) {
int gd = __gcd(numr, deno);
// Print the result
cout << numr / gd << " / "
<< deno / gd;
}
// If reccuring term exist
else {
// Convert reccuring term to integer
int reccu_num = stoi(reccu);
// reccu.size() is num of
// digit in reccur term
int numr1
= numr
* pow(10, reccu.size())
+ reccu_num;
int deno1 = deno
* pow(10, reccu.size());
// eq 2 - eq 1
int res_numr = numr1 - numr,
res_deno = deno1 - deno;
int gd = __gcd(res_numr,
res_deno);
// Print the result
cout << res_numr / gd << " / "
<< res_deno / gd;
}
}
// Driver Code
int main()
{
// Given string str
string str = "23.98(231)";
// Function Call
findFraction(str);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Recursive function to return
// gcd of a and b
static int __gcd(int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
// Function to convert the floating
// values into fraction
static void findFraction(String s)
{
// Initialize variables
String be_deci = "",
af_deci = "",
reccu = "";
boolean x = true, y = false,
z = false;
// Traverse the floating String
for(int i = 0; i < s.length(); ++i)
{
// Check if decimal part exist
if (s.charAt(i) == '.')
{
x = false;
y = true;
continue;
}
// Check if recurrence
// sequence exist
if (s.charAt(i) == '(')
{
z = true;
y = false;
continue;
}
// Retrive decimal part
// and recurrence resquence
if (x)
be_deci += s.charAt(i);
if (y)
af_deci += s.charAt(i);
if (z)
{
// Traverse the String
for(; i < s.length() && s.charAt(i) != ')';
++i)
reccu += s.charAt(i);
break;
}
}
// Convert String to integer
int num_be_deci = Integer.valueOf(be_deci);
int num_af_deci = 0;
// If no recurrence sequence exist
if (af_deci.length() != 0)
num_af_deci = Integer.valueOf(af_deci);
// Initialize numerator & denominator
int numr = num_be_deci *
(int)Math.pow(10, af_deci.length()) +
num_af_deci;
int deno = (int)Math.pow(10, af_deci.length());
// No reccuring term
if (reccu.length() == 0)
{
int gd = __gcd(numr, deno);
// Print the result
System.out.print(numr / gd + " / " +
deno / gd);
}
// If reccuring term exist
else
{
// Convert reccuring term to integer
int reccu_num = Integer.valueOf(reccu);
// reccu.size() is num of
// digit in reccur term
int numr1 = numr *
(int)Math.pow(10, reccu.length()) +
reccu_num;
int deno1 = deno * (int) Math.pow(
10, reccu.length());
// eq 2 - eq 1
int res_numr = numr1 - numr,
res_deno = deno1 - deno;
int gd = __gcd(res_numr,
res_deno);
// Print the result
System.out.print(res_numr / gd + " / " +
res_deno / gd);
}
}
// Driver Code
public static void main(String[] args)
{
// Given String str
String str = "23.98(231)";
// Function Call
findFraction(str);
}
}
// This code is contributed by Amit Katiyar
Python3
# Python3 program for the above approach
from math import gcd
# Function to convert the floating
# values into fraction
def findFraction(s):
# Initialize variables
be_deci = ""
af_deci = ""
reccu = ""
x = True
y = False
z = False
# Traverse the floating string
for i in range(len(s)):
# Check if decimal part exist
if (s[i] == '.'):
x = False
y = True
continue
# Check if recurrence
# sequence exist
if (s[i] == '('):
z = True
y = False
continue
# Retrive decimal part
# and recurrence resquence
if (x):
be_deci += s[i]
if (y):
af_deci += s[i]
if (z):
# Traverse the string
while i < len(s) and s[i] != ')':
reccu += s[i]
i += 1
break
# Convert to integer
num_be_deci = int(be_deci)
num_af_deci = 0
# If no recurrence sequence exist
if len(af_deci) != 0:
num_af_deci = int(af_deci)
# Initialize numerator & denominator
numr = (num_be_deci *
pow(10, len(af_deci)) +
num_af_deci)
deno = pow(10, len(af_deci))
# No reccuring term
if len(reccu) == 0:
gd = gcd(numr, deno)
# Print the result
print(numr // gd, "/", deno // gd)
# If reccuring term exist
else:
# Convert reccuring term to integer
reccu_num = int(reccu)
# reccu.size() is num of
# digit in reccur term
numr1 = (numr * pow(10, len(reccu)) +
reccu_num)
deno1 = deno * pow(10, len(reccu))
# eq 2 - eq 1
res_numr = numr1 - numr
res_deno = deno1 - deno
gd = gcd(res_numr, res_deno)
# Print the result
print(res_numr // gd, " / ",
res_deno // gd)
# Driver Code
if __name__ == '__main__':
# Given str
str = "23.98(231)"
# Function Call
findFraction(str)
# This code is contributed by mohit kumar 29
C#
// C# program for the above approach
using System;
class GFG{
// Recursive function to return
// gcd of a and b
static int __gcd(int a, int b)
{
return b == 0 ? a : __gcd(b, a % b);
}
// Function to convert the floating
// values into fraction
static void findFraction(String s)
{
// Initialize variables
String be_deci = "",
af_deci = "",
reccu = "";
bool x = true, y = false,
z = false;
// Traverse the floating String
for(int i = 0; i < s.Length; ++i)
{
// Check if decimal part exist
if (s[i] == '.')
{
x = false;
y = true;
continue;
}
// Check if recurrence
// sequence exist
if (s[i] == '(')
{
z = true;
y = false;
continue;
}
// Retrive decimal part
// and recurrence resquence
if (x)
be_deci += s[i];
if (y)
af_deci += s[i];
if (z)
{
// Traverse the String
for(; i < s.Length && s[i] != ')';
++i)
reccu += s[i];
break;
}
}
// Convert String to integer
int num_be_deci = Int32.Parse(be_deci);
int num_af_deci = 0;
// If no recurrence sequence exist
if (af_deci.Length != 0)
num_af_deci = Int32.Parse(af_deci);
// Initialize numerator & denominator
int numr = num_be_deci *
(int)Math.Pow(10, af_deci.Length) +
num_af_deci;
int deno = (int)Math.Pow(10, af_deci.Length);
// No reccuring term
if (reccu.Length == 0)
{
int gd = __gcd(numr, deno);
// Print the result
Console.Write(numr / gd + " / " +
deno / gd);
}
// If reccuring term exist
else
{
// Convert reccuring term to integer
int reccu_num = Int32.Parse(reccu);
// reccu.Count is num of
// digit in reccur term
int numr1 = numr *
(int)Math.Pow(10, reccu.Length) +
reccu_num;
int deno1 = deno * (int)Math.Pow(
10, reccu.Length);
// eq 2 - eq 1
int res_numr = numr1 - numr,
res_deno = deno1 - deno;
int gd = __gcd(res_numr,
res_deno);
// Print the result
Console.Write(res_numr / gd + " / " +
res_deno / gd);
}
}
// Driver Code
public static void Main(String[] args)
{
// Given String str
String str = "23.98(231)";
// Function Call
findFraction(str);
}
}
// This code is contributed by Amit Katiyar
798611 / 33300
时间复杂度: O(log 10 N)
辅助空间: O(1)