Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 3478 | Accepted: 1162 |
Description
To make this tenable, he assumes that the original fraction is always the simplest one that produces the given sequence of digits; by simplest, he means the the one with smallest denominator. Also, he assumes that he did not neglect to write down important digits; no digit from the repeating portion of the decimal expansion was left unrecorded (even if this repeating portion was all zeroes).
Input
Output
Sample Input
0.2... 0.20... 0.474612399... 0
Sample Output
2/9 1/5 1186531/2500000
Hint
Source
1 #include <stdio.h> 2 #include <iostream> 3 #include <algorithm> 4 #include <string.h> 5 #include <math.h> 6 #include <string> 7 #include <vector> 8 #include <queue> 9 #include <stack> 10 #include <set> 11 #include <map> 12 using namespace std; 13 typedef long long ll; 14 const int MAXN = 105; 15 const int INF = 0x3f3f3f3f; 16 char s[MAXN]; 17 //欧几里得求最大公因数 18 int gcd(int x, int y) 19 { 20 if (x<y) swap(x, y); 21 return y == 0 ? x : gcd(y, x%y); 22 } 23 //快速幂 24 int q_pow(int a, int b) 25 { 26 int r = 1, base = a; 27 while (b) 28 { 29 if (b & 1) r *= base; 30 base *= base; 31 b >>= 1; 32 } 33 return r; 34 } 35 int main(void) 36 { 37 while (scanf("%s", s) != EOF && strcmp(s, "0")) 38 { 39 int all = 0, cnt1 = 0; 40 int len = strlen(s); 41 for (int i = 2; i<len - 3; i++, cnt1++) 42 all = all * 10 + s[i] - '0'; 43 //all为 非循环节和循环节连起来的数 44 int mina = INF, minb = INF; //所求的分子与分母 45 for (int num = all / 10, cnt2 = cnt1 - 1; cnt2 >= 0; num /= 10, cnt2--) 46 { 47 //num为非循环节部分连起来的数 ,a为当前循环节下的分子,b为当前循环节下的分母 48 int a = all - num, b = q_pow(10, cnt2)*(q_pow(10, cnt1 - cnt2) - 1); 49 int g = gcd(a, b); 50 //求出分母最小的 51 if (b / g<minb) 52 { 53 minb = b / g; 54 mina = a / g; 55 } 56 } 57 printf("%d/%d\n", mina, minb); 58 } 59 return 0; 60 }
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <queue> 6 #include <vector> 7 #include <map> 8 #include <set> 9 #include <string> 10 #include <cmath> 11 using namespace std; 12 const int INF=0x3f3f3f3f; 13 typedef long long ll; 14 int gcd(int n,int m)//求最大公约数 15 { 16 if(m==0) return n; //n%m==0(n与m的余数为0) 17 return gcd(m,n%m);(n是大数,m是小数) 18 } 19 int main() 20 { 21 int all,num,l,m,n,a,b,k,mis,mns; 22 char str[100]; 23 while(gets(str)&&strcmp(str,"0")) 24 { 25 l=0;all=0;mis=INF; 26 for(int i=2;str[i]!='.';i++) 27 { 28 all=all*10+str[i]-48; 29 l++; 30 } 31 num=all; 32 for(int j=1;j<=l;j++) 33 { 34 num=num/10; 35 a=all-num; 36 b=(int)pow(10,l-j)*(pow(10,j)-1); 37 k=gcd(b,a); 38 if(b/k<mis) 39 { 40 mns=a/k; 41 mis=b/k; 42 } 43 } 44 printf("%d/%d\n",mns,mis); 45 } 46 return 0; 47 }
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 3478 | Accepted: 1162 |
Description
To make this tenable, he assumes that the original fraction is always the simplest one that produces the given sequence of digits; by simplest, he means the the one with smallest denominator. Also, he assumes that he did not neglect to write down important digits; no digit from the repeating portion of the decimal expansion was left unrecorded (even if this repeating portion was all zeroes).
Input
Output
Sample Input
0.2... 0.20... 0.474612399... 0
Sample Output
2/9 1/5 1186531/2500000
Hint
Source
1 #include <stdio.h> 2 #include <iostream> 3 #include <algorithm> 4 #include <string.h> 5 #include <math.h> 6 #include <string> 7 #include <vector> 8 #include <queue> 9 #include <stack> 10 #include <set> 11 #include <map> 12 using namespace std; 13 typedef long long ll; 14 const int MAXN = 105; 15 const int INF = 0x3f3f3f3f; 16 char s[MAXN]; 17 //欧几里得求最大公因数 18 int gcd(int x, int y) 19 { 20 if (x<y) swap(x, y); 21 return y == 0 ? x : gcd(y, x%y); 22 } 23 //快速幂 24 int q_pow(int a, int b) 25 { 26 int r = 1, base = a; 27 while (b) 28 { 29 if (b & 1) r *= base; 30 base *= base; 31 b >>= 1; 32 } 33 return r; 34 } 35 int main(void) 36 { 37 while (scanf("%s", s) != EOF && strcmp(s, "0")) 38 { 39 int all = 0, cnt1 = 0; 40 int len = strlen(s); 41 for (int i = 2; i<len - 3; i++, cnt1++) 42 all = all * 10 + s[i] - '0'; 43 //all为 非循环节和循环节连起来的数 44 int mina = INF, minb = INF; //所求的分子与分母 45 for (int num = all / 10, cnt2 = cnt1 - 1; cnt2 >= 0; num /= 10, cnt2--) 46 { 47 //num为非循环节部分连起来的数 ,a为当前循环节下的分子,b为当前循环节下的分母 48 int a = all - num, b = q_pow(10, cnt2)*(q_pow(10, cnt1 - cnt2) - 1); 49 int g = gcd(a, b); 50 //求出分母最小的 51 if (b / g<minb) 52 { 53 minb = b / g; 54 mina = a / g; 55 } 56 } 57 printf("%d/%d\n", mina, minb); 58 } 59 return 0; 60 }
1 #include <iostream> 2 #include <cstdio> 3 #include <algorithm> 4 #include <cstring> 5 #include <queue> 6 #include <vector> 7 #include <map> 8 #include <set> 9 #include <string> 10 #include <cmath> 11 using namespace std; 12 const int INF=0x3f3f3f3f; 13 typedef long long ll; 14 int gcd(int n,int m)//求最大公约数 15 { 16 if(m==0) return n; //n%m==0(n与m的余数为0) 17 return gcd(m,n%m);(n是大数,m是小数) 18 } 19 int main() 20 { 21 int all,num,l,m,n,a,b,k,mis,mns; 22 char str[100]; 23 while(gets(str)&&strcmp(str,"0")) 24 { 25 l=0;all=0;mis=INF; 26 for(int i=2;str[i]!='.';i++) 27 { 28 all=all*10+str[i]-48; 29 l++; 30 } 31 num=all; 32 for(int j=1;j<=l;j++) 33 { 34 num=num/10; 35 a=all-num; 36 b=(int)pow(10,l-j)*(pow(10,j)-1); 37 k=gcd(b,a); 38 if(b/k<mis) 39 { 40 mns=a/k; 41 mis=b/k; 42 } 43 } 44 printf("%d/%d\n",mns,mis); 45 } 46 return 0; 47 }