• So Hard (水题)


    题目网址:http://acm.fzu.edu.cn/problem.php?pid=2193


    Problem Description

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

    Input

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

    Output

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

    Sample Input

    20.50.4

    Sample Output

    1/22/5

    Hint

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

    Source

    福州大学第十二届程序设计竞赛


    考虑集中特殊情况即可。

    #include<iostream>
    #include<algorithm>
    #include <cstdio>
    #define INF 0x3f3f3f3f;
    using namespace std;
    int gcd(int a,int b){
    	if(b==0) return a;
    	int t=a%b;
    	while(t!=0){
    		a=b;
    		b=t;
    		t=a%b;
    	}
    	return b;
    }
    int pow(int x){
    	int n=1;
    	for(int i=0;i<x;i++){
    		n*=10;
    	}
    	return n;
    }
    int main (){
    	int T;
    	cin>>T;
    	while(T--){
    		 string str;
    		 cin>>str;
    		 int len=str.length();
    		 int mark=0,n=0,pot;
    		 for(int i=0;i<len;i++){
    		 	if( (mark==0&&str[i]=='0')  )
    		 	  continue;
    		 	if( str[i]=='.')
    		 	  pot=i;
    		 	else{
    		 	    n=n*10+str[i]-'0';
    				 mark=1;	
    		 	}
    		 }
    		 int y=pow(len-pot-1);
    		 int gg=gcd(n,y);
    		 //cout<<gg<<' '<<n<<'/'<<y<<endl;
    		 n=n/gg,y=y/gg;
    		 if(n==y)
    		   cout<<1<<endl;
    		 else if(y==1) cout<<n<<endl; 
    		 else
    		 cout<<n<<'/'<<y<<endl;	
    	}
     return 0;
    }
    





  • 相关阅读:
    ACM学习历程——POJ 1700 Crossing River(贪心)
    ACM学习历程——POJ 2376 Cleaning Shifts(贪心)
    ACM学习历程——HDU1331 Function Run Fun(锻炼多维dp的打表)
    hadoop-mapreduce-架构概念
    Java-笔记12
    Java-笔记11-复习
    机器学习概述
    hadoop-hdfs-API
    hadoop-
    Java-Project-02
  • 原文地址:https://www.cnblogs.com/zswbky/p/5431952.html
Copyright © 2020-2023  润新知