Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 69 Accepted Submission(s): 27
1. 0 < C < D < B, and
2. the error |A/B - C/D| is the minimum over all possible values of C and D, and
3. D is the smallest such positive integer.
1. B is a 32 bit integer strictly greater than 2, and
2. 0 < A < B
分解成连分数,然后最后一个数减一;
//HDU 4180 连分数 #include<stdio.h> #include<math.h> #include<algorithm> using namespace std; int an[100]; int gcd(int a,int b) { int r=0; while(b!=0) { r=a%b; a=b; b=r; } return a; } int fenjie(int a,int b,int an[])//连分数分解 { int n=0; int t; while(a!=1) { an[n++]=b/a; t=b%a; b=a; a=t; } an[n++]=b; return n; } int main() { int n,A,B,C,D; while(scanf("%d",&n)!=EOF) { for(int i=0;i<n;i++) { scanf("%d/%d",&A,&B); int d=gcd(A,B); if(d>1) { printf("%d/%d\n",A/d,B/d); continue; } int len=fenjie(A,B,an); an[len-1]--; C=1; D=an[len-1]; for(int j=len-2;j>=0;j--) { int t=an[j]*D+C; C=D; D=t; } printf("%d/%d\n",C,D); } } return 0; }