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.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.
Sample Input 1:0.142857 6

Sample Output 1:1/7

Sample Input 2 :1.6 1

Sample Output 2:5/3

Sample Input 3 :123.456 2

Sample Output 3:61111/495

这个题的重点在于要知道无限循环小数如何转化为分数,转化方法源自百度:

一、从小数点后e799bee5baa6e997aee7ad94e4b893e5b19e31333366306435就开始的循环小数化成分数:例如把0.4747……化成分数。

(1)0.4747……×100=47.4747……

(2)0.4747……×100-0.4747……=47.4747……-0.4747……

(3)(100-1)×0.4747……=47

(4)99×0.4747…… =47

(5)0.4747……=47/99

二、间隔几位的循环小数化分数:例如把0.325656……化成分数。

(1)0.325656……×100=32.5656……①

(2)0.325656……×10000=3256.56……②

(3)用②-①即得:0.325656……×9900=3256.5656……-32.5656……

(4)0.325656……×9900=3256-32

(5)0.325656……=3224/9900

扩展资料:

简单小数化分数的方法:

1、首先看小数点后面有几位数,如果是2位就除以100,是1位除以10,三位数除以1000,以此类推。

2、然后分子和分母约分到不能再约分为止。

3、拿0.12做列子,变成12/100,上下可以用4约分,变成3/25.

小数的大小比较:先看整数部分,整数部分较大的,这个数就大;整数部分相同就看十分位,十分位较大的,这个数就大;十分位相同就看百分位,百分位较大的,这个数就大。以此类推。

有了转化方法以后剩下的就是打码环节了:

下面是AC代码:

#include 
using namespace std;
typedef long long ll;
ll FF(string a)用于把一个数字字符串变成数字
{
    ll ans=0;int len=a.size();
    for(int i=len-1;i>=0;i--)ans+=(a[i]-'0')*pow(10,len-i-1);
    return ans;
}
int main()
{
    string n;cin>>n;
    int m;cin>>m;
	int flag=0,len=n.size();
    for(int i=0;i=len-1-flag-m;i--)numbers_3+=9*pow(10,i);
    if(len-flag-1==m)
    {
        ll ans=__gcd(numbers_2,numbers_3);
        cout<

你可能感兴趣的:(个人题解)