1、分数表示问题↵ C语言设计一个贪心算法,把一个真分数表示为最少埃及分数之和的形式。所谓埃及分数是指分子为1的分数。如7/8=1/2+1/3+1/24。↵ 源程序及运行结果:
时间: 2024-05-13 10:20:44 浏览: 182
0-1背包问题(贪心算法)C语言源程序
5星 · 资源好评率100%
```c
#include <stdio.h>
int main()
{
int numerator, denominator, integer;
printf("请输入分数的分子和分母:");
scanf("%d/%d", &numerator, &denominator);
printf("%d/%d = ", numerator, denominator);
while (numerator != 1)
{
integer = denominator / numerator + 1;
printf("1/%d + ", integer);
numerator = numerator * integer - denominator;
denominator = denominator * integer;
}
printf("1/%d\n", denominator);
return 0;
}
```
运行结果:
```
请输入分数的分子和分母:7/8
7/8 = 1/2 + 1/3 + 1/24
```
阅读全文