标签:
关于小数话分数的知识点在学习与借鉴里面、
直接放代码了
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<cmath> 5 using namespace std; 6 int gcd(int a,int b) 7 { 8 if(b==0) return a; 9 return gcd(b,a%b); 10 } 11 int main() 12 { 13 int n;cin >> n;cin.get(); 14 while(n--){ 15 int mu1,mu2,zi1,zi2; 16 mu1=mu2=1; 17 zi1=zi2=0; 18 int flag=1; 19 char s[20]; 20 cin >> s; 21 for(int i=2;i<strlen(s);++i){ 22 if(s[i]==‘(‘) flag=0; 23 if(isdigit(s[i])){ 24 if(flag){ 25 mu1*=10; 26 zi1=zi1*10+s[i]-‘0‘; 27 } 28 mu2*=10; 29 zi2=zi2*10+s[i]-‘0‘; 30 } 31 } 32 if(flag){ 33 int digit=gcd(mu1,zi1); 34 printf("%d/%d\n",zi1/digit,mu1/digit); 35 } 36 else{ 37 printf("%d/%d\n",(zi2-zi1)/gcd(mu2-mu1,zi2-zi1),(mu2-mu1)/gcd(mu2-mu1,zi2-zi1)); 38 } 39 } 40 }
标签:
原文地址:http://www.cnblogs.com/sasuke-/p/5155311.html