标签:ade esc can string onclick 超过 end main word
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1717
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5375 Accepted Submission(s): 2188
题目大意: 有限小数, 循环小数化最简分数
解法思路:
纯循环小数:用循环节作分子,9999...9(循环节是几位就有几个9)作分母即可。
混循环小数:用第二个循环节以前的小数部分所组成的数,减去不循环部分所得的差,以这个差作为分数的分子;用999...9000...0做分子(9的位数是你取用的循环节的位数,0的位数是非循环部分的位数)。
代码实现:
1 #include <bits/stdc++.h> 2 using namespace std; 3 typedef long long ll; 4 int t; 5 string s; 6 7 void print(ll a, ll b) 8 { 9 ll gcd = __gcd(a, b); 10 a /= gcd; b /= gcd; 11 printf("%lld/%lld\n", a, b); 12 } 13 14 void solve() 15 { 16 cin >> s; 17 int p = -1, q = -1; 18 for (int i = 2; i < (int)s.size(); ++ i) { 19 if(s[i]==‘(‘) p = i; 20 if(s[i]==‘)‘) q = i; 21 } 22 if(p == -1) 23 { 24 ll x = 0, y = 1; 25 for (int i = 2; i < (int)s.size(); ++ i) x=x*10+s[i]-‘0‘, y*=10; 26 print(x, y); 27 } 28 else if (p == 2) 29 { 30 ll x = 0, y = 0; 31 for (int i = p+1; i < q; ++ i) x=x*10+s[i]-‘0‘; 32 for (int i = 1; i <= q-p-1; ++ i) y=y*10+9; 33 print(x, y); 34 } 35 else 36 { 37 ll x = 0, y = 0; 38 for (int i = 2; i < p; ++ i) x=x*10+s[i]-‘0‘; 39 y = x; 40 for (int i = p+1; i < q; ++ i) y=y*10+s[i]-‘0‘; 41 ll ans = 0; 42 for (int i = 1; i <= q-p-1; ++ i) ans=ans*10+9; 43 for (int i = 2; i < p; ++ i) ans*=10; 44 print(y-x, ans); 45 } 46 } 47 48 int main() { 49 scanf("%d", &t); 50 while(t--) { 51 solve(); 52 } 53 return 0; 54 }
标签:ade esc can string onclick 超过 end main word
原文地址:http://www.cnblogs.com/nuc-gxg/p/7450093.html