一个分数假如 3/5=1/(1+2/3)=1/(1+1/(1+1/2));
当分子出现1的时候,只要让分母减一。
#include <stdio.h> #include <stdlib.h> #include <math.h> #define EX(a, b) (a = a ^ b, b = a ^ b, a = a ^ b) using namespace std; int gcd(int a, int b) { return (b == 0) ? a : gcd(b, a % b); } class fenshu { public: int x, y; fenshu friend operator + (fenshu n, fenshu m) { m.x *= n.y; n.x *= m.y; m.y *= n.y; n.y = m.y; n.x += m.x; int k = gcd(n.x, n.y); n.x /= k; n.y /= k; return n; } fenshu friend operator / (int k, fenshu n) { EX(n.x, n.y); n.x *= k; return n; } }; fenshu ans_get(fenshu now) { if(now.x <= 1) { --now.y; return now; } else { EX(now.x, now.y); //小数倒置 fenshu s; s.x = now.x / now.y; s.y = 1; //分离出来的整数 now.x = now.x % now.y; return 1 / (s + ans_get(now)); }//继续分离 } void solve() { int T; scanf("%d", &T); while(T--) { fenshu now, s; scanf("%d/%d", &now.x, &now.y); now = ans_get(now); printf("%d/%d\n", now.x, now.y); } } int main() { solve(); return 0; }
时间: 2024-09-30 02:59:21