贪心法——埃及分数

问题

在这里插入图片描述

代码

/*
* problem: 埃及分数
* method: 贪心法
* date: 2020/05/11
*/
#include<iostream>
using namespace std;
int CommFactor(int a,int b) {
 int c=b;
 while(a%b){
  c=a%b;
  a=b;
  b=c;
 }
 return c;
}
void EgyptFactor(int a,int b) {
 int e,r;
 while(a>1) {
  e=b/a+1;
  cout<<"1/"<<e<<"+";
  a=a*e-b;
  b=b*e;
  r=CommFactor(a,b);
  if(r>1){
   a/=r;
   b/=r;
  }
 }
 cout<<"1/"<<b<<endl;
}
int main() {
 EgyptFactor(7,8);
 return 0;
}

猜你喜欢

转载自blog.csdn.net/The_Only_God/article/details/106086844