埃及分数问题(JLNUOJ 2372)


#include<iostream>//埃及分数问题 (egypt)分数 简单实现 #include<cstdio> using namespace std; void egypt(int a,int b) { //判断特例 if(a==1 || b%a==0) { cout<<b/a<<" "<<endl; return; } while(1) { int c = b / a + 1; //c 为最大的埃及分数 cout<<c<<" "; a = a*c - b;//new 分子 b = b*c;//new 分母 if(a==1||b%a==0) { cout<<b/a<<" "; break; } } cout<<endl; } int main() { ios::sync_with_stdio(false); int a,b;//a代表分子,b代表分母 while(cin>>a>>b) { egypt(a,b); } }

埃及分数(贪心的思想)

真分数 A /  B

B = A X D + K

B / A = D + K / A < D + 1

A / B > 1/(D + 1)

A/B - 1/C = (AXC -B) / (BXC)

因此可得最大的埃及分数为 B / A + 1

猜你喜欢

转载自www.cnblogs.com/newstartCY/p/11432351.html