So Hard
請將有限小數化為最簡分數。
Input
一個整數n 表示需要轉化的小數個數; 接下來n行,每行有一個有限小數。(保證小數位數不超過9位)
Output
輸出有n行,每行為小數對應的最簡分數
Sample Input
2
0.5
0.4
Sample Output
1/2
2/5
Hint
注意精度問題,數據保證不會有類似1.0的小數。
思路:這題輸入主要要字符串,在把他們轉數字,在分別求公約數,數據范圍long long
代碼:
#include<cstdio> #include<iostream> #include<algorithm> #include<cstring> using namespace std; long long n; long long gcd(long long m,long long n) {return n==0?m:gcd(n,m%n); } int main() {long long t;scanf("%lld\n",&t);while(t--){char s[1000];long long flag=0,sum=1,n=0,ans=0;scanf("%s",s);int len=strlen(s);for(int i=0;i<len;i++){if(s[i]=='.'){flag=1;continue;}if(flag==1){sum=sum*10;}n=n*10+(s[i]-'0');}long long g=gcd(n,sum);long long p=n/g;long long q=sum/g;//if(s[0]=='-'&&p!=0)printf("-");printf("%I64d/%I64d\n",p,q);}return 0; } 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
- 上一篇: ElasticSearch highli
- 下一篇: zoj2196