题解 | #将真分数分解为埃及分数#

将真分数分解为埃及分数

https://www.nowcoder.com/practice/e0480b2c6aa24bfba0935ffcca3ccb7b

//只说输出成埃及分数,没说一定得不一样
#include <stdio.h>
#include <string.h>
#include <math.h>
int main() {
    int a, b;
    char str[10];
    while (scanf("%s", str) != EOF ) {
        int len = strlen(str);
        int fs[2] = {0};
        int x = 0, z = 0;
        for (int i = len-1; i >= 0; i--) {
            if (str[i] == '/') {
                x++;
                z = 0;
                continue;
            }
            fs[x] = (str[i] - '0')* pow(10, z++) + fs[x];
            // printf("%d\n",fs[0]);
            // printf("%d\n",fs[1]);
        }
        //printf("%d/%d\n",fs[0],fs[1]);
            for (int i = 0; i < fs[1] - 1; i++) {
                printf("1/%d+", fs[0]);
            }
            printf("1/%d\n", fs[0]);
        }

    }




    /*运算超时
    #include <math.h>
    #include <stdio.h>
    #include <string.h>
    int t = 2;
    float y = 0.5;


    void aj(float a) {
        if ((a - y) > 0) {
            a = a - y;
            //printf("%f\n",y);
            printf("1/%d+", t);
            t++;
            y = pow(t, -1);
            aj(a);
        } else if ((a - y) < 0) {
            t++;
            y = pow(t, -1);
            aj(a);
        } else if ((a - y) == 0) {
            printf("1/%d\n", t);
            t = 2;
            y = 0.5;
            return;
        }


    }

    int main() {
        char str[10];
        while (scanf("%s", str) != EOF ) {
            int len = strlen(str);
            float fs[2] = {0};
            int x = 0, z = 0;
            for (int i = 0; i < len; i++) {
                if (str[i] == '/') {
                    x++;
                    z = 0;
                    continue;
                }
                fs[x] = (str[i] - '0') + fs[x] * pow(10, z++);
                // printf("%d\n",fs[0]);
                // printf("%d\n",fs[1]);
            }
            //printf("%d/%d\n",fs[0],fs[1]);
            float num = fs[0] / fs[1];

            aj(num);


        }


        return 0;
    }**/

全部评论

相关推荐

2025-12-18 12:37
南京邮电大学 C++
牛客87317764...:然后客户端边学边投,学个1个月投不进去都是正常的,这玩意非常看运气
投递快手等公司9个岗位
点赞 评论 收藏
分享
2025-12-15 11:27
门头沟学院 Java
哇哇的菜鸡oc:所有人不要理会,就好了,后面他就知道怎么回事了,只能说有的时候市场都是被宰的人搞坏的
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务