sm5141 2023-05-28 18:32 采纳率: 0%
浏览 34

人工方式下比较分数大小最常用的方法是:进行分数的通分后比较分子的大小。

人工方式下比较分数大小最常用的方法是:进行分数的通分后比较分子的大小。编程实现模拟该过程,其中计算分母a和b的最小公倍数的函数定义形式如下:
int LCM(int a, int b)
{
}

**输入格式要求:"%d/%d,%d/%d" 提示信息:"Input two fractions\n"
**输出格式要求:"%d/%d>%d/%d\n"或者 "%d/%d=%d/%d\n"或者 "%d/%d<%d/%d\n"
运行结果示例:
Input two fractions
4/5,6/7
4/5<6/7

  • 写回答

1条回答 默认 最新

  • threenewbee 2023-05-28 21:35
    关注
    #include <stdio.h>
     
    int LCM(int a, int b)  
    {
        int min_common_factor;  
        int temp;
     
        // 计算 a 和 b 的公因数  
        for (temp = 1; temp < a; temp++)  
            for (temp < b; temp++)  
                if (temp * temp <= a * b)  
                    break;
     
        // 如果存在公因数,则将其加到 a 和 b 的乘积中  
        if (temp * temp > a * b)  
            for (temp = a; temp * temp <= b; temp++)  
                b = temp;  
        else if (temp * temp < a * b)  
            for (temp = b; temp * temp <= a; temp++)  
                a = temp;
     
        // 返回最小公倍数  
        return min_common_factor;  
    }
     
    int main()  
    {
        char direction;  
        int a, b, d1, d2;  
        int min_common_factor;
     
        printf("Input two fractions:\n");  
        scanf("%d/%d,%d/%d", &a, &b, &d1, &d2);
        
        // 计算最小公倍数  
        min_common_factor = LCM(b, d2);
     
        // 判断方向 (大于、等于、小于)  
        if (min_common_factor / b * a > min_common_factor / d2 * b)  
            direction = '>';  
        else if (min_common_factor / b * a < min_common_factor / d2 * b)  
            direction = '<';  
        else  
            direction = '=';
     
        // 输出结果  
        printf("%d/%d%c%d/%d\n", a, b, direction, d1, d2);  
        return 0;  
    }
    
    评论 编辑记录

    报告相同问题?

    问题事件

    • 创建了问题 5月28日

    悬赏问题

    • ¥15 西门子plc6ES7217-1AG40-0XB0怎么rs485通讯呀!
    • ¥15 matlab有svec这个函数吗?
    • ¥15 无法调用VideoWriter_fourcc
    • ¥15 VB6.0无法加载网页验证码图片到picturebox中,求解。
    • ¥15 C#和GDAL对栅格处理
    • ¥15 我现在有一些关于提升机故障的专有文本数据,量也不多,我在label studio上进行了关系和实体的标注,完成了知识图谱的构造,那么我使用生成式模型的话,我能做哪些工作来写我的论文?
    • ¥15 电脑连不上无线网络如下诊断反馈应该如何操作
    • ¥15 telegram api 使用forward_messages方法转发消息时,目标群组里面会出现此消息来源,如何隐藏?
    • ¥15 关于#tensorflow#的问题:有没有什么方法可以让机器自己学会像素风格的图片
    • ¥15 Oracle触发器字段变化时插入指定值