zoukankan      html  css  js  c++  java
  • Rational Ratio

    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.1428566 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;
    }
  • 相关阅读:
    利用KINECT+OPENCV检测手势的演示程序
    关于PPC软件的开发库
    FlashGet的IE加载项与IE8不兼容
    ubuntu9.04 安装字体 后记
    【pys60笔记】中文
    【pys60学习笔记】S60 模拟器使用。
    ubuntu9.04 安装字体
    IDE硬盘安装Windows 7 7106(光驱与硬盘共用一个IDE)
    易歌——web在线听歌桌面程序。带全局键控制。
    MapX直连Oracle——MapX5配合Oracle时,对中文表名支持不好
  • 原文地址:https://www.cnblogs.com/dealer/p/12811321.html
Copyright © 2011-2022 走看看