• 有限小数化为最简分数


    /*

    请将有限小数化为最简分数。

    Input

    一个整数n 表示需要转化的小数个数; 接下来n行,每行有一个有限小数。(保证小数位数不超过9位)

    Output

    输出有n行,每行为小数对应的最简分数

    Sample Input

    2
    0.5
    0.4
    

    Sample Output

    1/2 2/5

    注意精度问题,数据保证不会有类似1.0的小数。

    */

    #include <cstdio>
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    using namespace std;
    int Gcd(int a,int b){
        if(a<b){
            int t;
            t = a;
            a = b;
            b = t;
        }
        return b!=0?Gcd(b,a%b):a;
    }
    char str[50],str2[50];
    int main() {
        int t,i,k,count,big,small,mul1,mul2,gcd,sum;
        cin>>t;
        while(t--)
            {
            cin>>str;
            count = 0;
            int len = strlen (str);
            for(i=0; i< len;i++){
                if(str[i]!='.'){
                    count++;

                }
                else{
                    break;
                }
            }

            mul1 = mul2 = 1;
            sum = big = small = 0;
            for(i=0; i<count; i++){//整数

                big = str[i]-'0'+ big*10;
                mul1 *= 10;
            }
            for(i=count+1; i<strlen(str); i++){ //小数

                small = str[i]-'0'+ small*10;
                mul2 *= 10;
            }

            big*=mul2;
            sum+=(big + small);
            gcd = Gcd(sum,mul2);
            cout<<sum/gcd<<'/'<<mul2/gcd<<endl;

        }
        return 0;
    }

  • 相关阅读:
    matlab 使用OPENCV
    MATLAB SVM
    RestClient POST
    IIS HTTPS 禁用不安全的SSL2.0
    ping + 时间 日志
    matlab 文件遍历
    matlab 投影
    Oracle创建表空间、创建用户以及授权、查看权限
    php使用<?php include之后页首有空白
    sql点滴40—mysql乱码问题总结
  • 原文地址:https://www.cnblogs.com/jxust-jiege666/p/6424503.html
Copyright © 2020-2023  润新知