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

将真分数分解为埃及分数

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

#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void intochar(unsigned long a, char str[]) {
    unsigned long count = 1;
    unsigned long k = 1;
    unsigned long temp = a;
    while (temp / 10 != 0) {
        count *= 10;
        k++;
        temp = temp / 10;
    }
    for (int i = 0; i < k; i++) {
        str[i] = a / count+'0';
        a = a % count;
        count = count / 10;
    }
}

int main() {
    char str[2][10];
    unsigned long cou[2];
    char res[100][10] = {0};
    while (~scanf("%ld/%ld", &cou[0],&cou[1])) {
        
        int k = 0;
        char ress1[20];
        unsigned long mu = 0;
        memset(ress1, 0, sizeof(ress1));
        
        while (cou[1] % cou[0] != 0) {
            
            char zi[10] = "1/";
            mu = cou[1] / cou[0] + 1;
            intochar(mu, ress1);
            strcpy(res[k], strcat(zi, ress1));
            k++;
            int temp_cou1 = cou[1];
            cou[1] = mu * cou[1];
            cou[0] = cou[0] * mu - temp_cou1;
            memset(ress1, 0, sizeof(ress1));
        }
        char ress2[20];
        memset(ress2, 0, sizeof(ress2));
        intochar(cou[0], ress1);
        intochar(cou[1], ress2);
        strcat(strcat(ress1, "/"), ress2);
        if (cou[1] % cou[0] == 0) {
            strcpy(res[k], ress1);
        }

        printf("%s",res[0]);
        for (int i = 1; i <= k; i++) {
            printf("+%s", res[i]);
        }
        printf("\n");

    }
}

全部评论

相关推荐

点赞 评论 收藏
分享
10-30 23:23
已编辑
中山大学 Web前端
去B座二楼砸水泥地:这无论是个人素质还是专业素质都👇拉满了吧
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务