题解 | #将真分数分解为埃及分数#
将真分数分解为埃及分数
http://www.nowcoder.com/practice/e0480b2c6aa24bfba0935ffcca3ccb7b
//C++正统解法,调用递归 #include<iostream> #include<string> using namespace std; void Func(int son,int mom,int index) { if(son==0) return; if(mom%son==0) { cout<<"+1"<<"/"<<mom/son<<endl; return; } while(index*son<mom) index++; cout<<"+1/"<<index; Func(index*son-mom, index*mom, index); return; } int main() { string str; while(cin>>str) { int len=str.length(); int s=0,m=1; string str1,str2; for(int i=0;i<len;++i) { if(str[i]=='/') { str1=str.substr(0,i); str2=str.substr(i+1,len-1-i); break; } } s=atoi(str1.c_str()); m=atoi(str2.c_str()); int index=2; while(index*s<m) index++; cout<<"1/"<<index; Func(s*index-m, index*m,index+1); } return 0; }