/****************************************************************************/ |读初等数论及应用: 想着好玩,就实现了比较简单,埃及分数: 5/7 = 1/2 + 1/7 + 1/70 /****************************************************************************/ #include<iostream> #include<cmath> using namespace std; const int maxn = 100; int ans[maxn]; //求x y 的最大公约数 int gcd(int x, int y) { if(!x || !y){return x>y ? x:y;} for(int t; t=x%y; x=y, y=t); return y; } //模拟埃及分数的计算 int solve(int a, int b) { int k = 0; while(a!=1) { ans[k] = floor(b*1.000/a) + 1; a = a*ans[k] - b; b = b*ans[k]; int x = gcd(a, b); a = a/x; b = b/x; k++; } ans[k++] = b; return k; } int main() { int a, b; while(scanf("%d%d", &a, &b)!=EOF) { memset(ans, 0, sizeof(ans)); int k = solve(a, b); //printf("%d/n", k); printf("%d/%d = ", a, b); for(int i=0; i<k; i++) { if(i!=k-1) printf("1/%d + ", ans[i]); else printf("1/%d/n", ans[i]); } } return 0; };