现输入一个真分数,请将该分数分解为埃及分数。
#include<iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"请分别输入一个真分数的分母和分子:"<<endl;
cin>>a>>b;
cout<<"分解成埃及分数为:";
while(1)
{
if(b%a)
{
c=b/a+1;
}
else
{
c=b/a;
a=1;
}
if(a==1)
{
cout<<"1/"<<c<<endl;
break;
}
else
{
cout<<"1/"<<c<<"+";
}
a=a*c-b;
b=b*c;
if(a==3&&b%2==0)
{
cout<<"1/"<<b/2<<"+"<<"1/"<<b;
break;
}
}
return 0;
}
运行结果为:
标签:分数,b%,真分数,cout,else,break,分解 From: https://www.cnblogs.com/618zyk/p/17421607.html