题解 | #将真分数分解为埃及分数#
将真分数分解为埃及分数
http://www.nowcoder.com/practice/e0480b2c6aa24bfba0935ffcca3ccb7b
这道题就是有个机制不停的更新分子和分母。
#include <algorithm>
#include <sstream>
using namespace std;
int main() {
string str;
while(getline(cin,str)){
stringstream ss;
ss.str(str);
string denominator,molecule;
getline(ss,molecule,'/');
getline(ss, denominator);
int a = stoi(molecule);
int b = stoi(denominator);
int q,r,p;
while(a!=1){
if(b%(a-1)==0){//不加这个的话智能通过18个样例
cout<<1<<'/'<<b/(a-1)<<'+';
break;
}
q = b/a;
r = b%a;
p = q+1;
cout<<1<<'/'<<p<<'+';
a = a-r;
b = b*p;
if(b%a==0){
b = b/a;
break;
}
}
cout<<1<<'/'<<b<<endl;
}
}