ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Rational Ratio

2020-04-30 21:57:30  阅读:264  来源: 互联网

标签:digits Ratio int down2 number len Rational include


Every (positive) rational number can be expressed as a ratio of two (positive) integers.However, in decimal form, rational numbers often have an infinitely repeating pattern, e.g., 1/7 = 0.1428571428571428571/7=0.142857142857142857... A convenient way of writing this repeating pattern is to put a bar over the first occurrence of the repeating part, so 1/71/7 would be written:

 

0.\overline{142857}.0.142857.

 

Given a rational number consisting of a series of digits, followed by a decimal point, followed by more digits, and then a number indicating how many of the rightmost digits repeat (i.e., the number of digits under the bar), your task is to find the ratio of two integers, in the most reduced form, that represent the same rational number. For example, for the input 0.1428570.142857 66 you should find 1/71/7.

Input

The input will be a single line with two numbers separated by one space. The first number will consist of 11 to 33 digits (00--99), followed by a decimal point, followed by 11 to 1111 digits (00--99), representing the decimal form of the number, possibly with leading zeros. The second number will be a positive integer indicating how many of the rightmost digits of the preceding number repeat. The first number will always be greater than 00. The second number will never be less than 11 nor larger than the number of digits to the right of the decimal point.

Output

Print the corresponding fraction in its most reduced form, that is, the fraction with the smallest possible integer values in the numerator and denominator.

输出时每行末尾的多余空格,不影响答案正确性

样例输入1

0.142857 6

样例输出1

1/7

样例输入2

1.6 1

样例输出2

5/3

样例输入3

123.456 2

样例输出3

61111/495

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
#include <set>
#include <queue>
#include <map>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <numeric>
#include <cmath>
#include <iomanip>
#include <deque>
#include <bitset>
//#include <unordered_set>
//#include <unordered_map>
//#include <bits/stdc++.h>
//#include <xfunctional>
#define ll              long long
#define PII             pair<int, int>
#define rep(i,a,b)      for(int  i=a;i<=b;i++)
#define dec(i,a,b)      for(int  i=a;i>=b;i--)
#define pb              push_back
#define mk              make_pair
using namespace std;
int dir1[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,1 },{ -1,1 } };
int dir2[6][2] = { { 0,1 } ,{ 0,-1 },{ 1,0 },{ -1,0 },{ 1,-1 },{ -1,-1 } };
const long long INF = 0x7f7f7f7f7f7f7f7f;
const int inf = 0x3f3f3f3f;
const double pi = 3.14159265358979;
const int mod = 1000000007;
const int N = 1005;
//if(x<0 || x>=r || y<0 || y>=c)

inline ll read()
{
    ll x = 0; bool f = true; char c = getchar();
    while (c < '0' || c > '9') { if (c == '-') f = false; c = getchar(); }
    while (c >= '0' && c <= '9') x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
    return f ? x : -x;
}
ll gcd(ll m, ll n)
{
    return n == 0 ? m : gcd(n, m % n);
}

ll tonum(string s)
{
    ll res = 0;
    for (int i = 0; i < s.size(); i++)
    {
        res = res * 10 + s[i] - '0';
    }
    return res;
}
int main()
{
    string s, s1;
    ll pos,up1,down1=1,down2 = 1,len,d;
    cin >> s>>len;
    s1 = s.substr(s.size() - len, len);
    d = tonum(s1);
    for (int i = 0; i < s.size() - len; i++)
    {
        if (s[i] == '.')
        {
            pos = i;
            break;
        }
    }
    ll a;
    a = tonum(s.substr(0, pos));
    up1 = tonum(s.substr(pos + 1, s.size() - pos - 1 - len));
    for (int i = 0; i < s.size() - pos - 1 - len; i++)
        down1 *= 10;
    for (int i = 0; i < len; i++)
        down2 *= 10;
    down2--;
    ll g = gcd(down2, d);
    d /= g;
    down2 /= g;
    ll res1, res2;
    res1 = up1*down2 + d;
    res2 = down1*down2;
    g = gcd(res1, res2);
    res1 /= g;
    res2 /= g;
    res1 += a*res2;
    cout << res1 << "/" << res2 << endl;
    return 0;
}

 

标签:digits,Ratio,int,down2,number,len,Rational,include
来源: https://www.cnblogs.com/dealer/p/12811321.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有