Sweety

Practice makes perfect

导航

So Hard (水题)

Posted on 2015-05-03 22:00  蓝空  阅读(112)  评论(0编辑  收藏  举报

题目网址: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;
}