题解 | #将真分数分解为埃及分数#
将真分数分解为埃及分数
https://www.nowcoder.com/practice/e0480b2c6aa24bfba0935ffcca3ccb7b
#include <iostream> using namespace std; #include<string> #include<vector> #include<cmath> int main() { string s; while (cin >> s) { long int a, b; //修改为long int型,保证最后一个案例的输出 int pos = s.find('/'); a = stoi(s.substr(0, pos)); b = stoi(s.substr(pos + 1)); vector<long int>re; long int c; while (a != 1) { //a=1的时候需要输出最后一个数b if (b % a == 0) { b = b / a; break; } else { c = b / a + 1; re.push_back(c); a = a * c - b; b = b * c; } } for (vector<long int>::iterator it = re.begin(); it != re.end(); it++) { cout << "1/" << *it << '+'; } cout << "1/" << b << endl; } return 0; } // 64 位输出请用 printf("%lld")