题解 | #将真分数分解为埃及分数#
将真分数分解为埃及分数
https://www.nowcoder.com/practice/e0480b2c6aa24bfba0935ffcca3ccb7b
#include <iostream> #include <string> using namespace std; int st2i(string s){ int x=0; for(char ch:s){ x=10*x+ch-'0'; } return x; } // b=a*x+y // a/b=1/(x+1)+(a-y)/(ax+y)(x+1) int main() { string s1; while(cin>> s1){ int index=s1.find('/'); string a1=s1.substr(0,index); string b1=s1.substr(index+1); long a=st2i(a1); long b=st2i(b1); while(a!=1){ if(b%a==0){ b/=a; break; } long x=b/a; long y=b%a; cout << "1/" << x+1<<'+'; a=a-y; b=b*(x+1); } cout << "1/" << b << endl; } } // 64 位输出请用 printf("%lld")