c - 在两个文件中添加两个数字,然后将其写入另一个文件。数字太大,不适合整数范围

标签 c file-io amazon

这是亚马逊问的面试问题。
给定两个非常大的数字,即它们不适合两个不同文件中的'C'lang整数范围。告诉我两个数字的总和。

最佳答案

这是一种实现方式,可以修改为从两个文件中读取数字字符串,而不是通过scanf从标准输入中读取。这将读取最长为MAX_LEN的数字字符串。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_LEN 10000

int main(int argc, char **argv)
{
    /* read in first string */
    char *fs = NULL;
    fs = malloc(MAX_LEN);
    if (!scanf("%s", fs)) {
        fprintf(stderr, "Error: Could not read first string!\n");
        return EXIT_FAILURE;
    }
    size_t fs_len = strlen(fs);

    /* populate result array with first string digits */
    short *res = NULL;
    int res_len = MAX_LEN + 1;
    res = malloc(sizeof(short) * res_len);
    for (int res_idx = res_len - 1; res_idx >= 0; res_idx--) {
        if (res_idx > (fs_len - 1)) {
            res[res_idx] = 0;
            continue;
        }
        int digit = fs[fs_len - res_idx - 1] - (int)'0';
        res[res_idx] = digit;
        /* error checking */
        if ((res[res_idx] < 0) || (res[res_idx] > 9)) {
            fprintf(stderr, "Error: Bad digit in fs at index %lu\n", fs_len - res_idx - 1);
            return EXIT_FAILURE;
        }
    }
    free(fs), fs = NULL;

    /* read in first string */
    char *ss = NULL;
    ss = malloc(MAX_LEN);
    if (!scanf("%s", ss)) {
        fprintf(stderr, "Error: Could not read second string!\n");
        return EXIT_FAILURE;
    }
    size_t ss_len = strlen(ss);

    /* do the summation */
    for (int ss_idx = ss_len - 1, res_idx = 0; ss_idx >= 0; ss_idx--, res_idx++) {
        int digit = ss[ss_idx] - (int)'0';
        /* error checking */
        if ((digit < 0) || (digit > 9)) {
            fprintf(stderr, "Error: Bad digit in ss at index %d\n", ss_idx);
            return EXIT_FAILURE;
        }
        int temp_res = res[res_idx] + digit;
        /* do we need to carry up? */
        if (temp_res >= 10) {
            res[res_idx + 1] += 1;
            res[res_idx] = temp_res - 10;
        }
        else {
            res[res_idx] = temp_res;
        }
    }
    free(ss), ss = NULL;

    /* print result */
    for (int res_idx = (fs_len > ss_len ? fs_len : ss_len); res_idx >= 0; res_idx--) {
        fprintf(stdout, "%d", res[res_idx]);
    }
    fprintf(stdout, "\n");
    free(res), res = NULL;

    return EXIT_SUCCESS;
}


它最多只能在内存中保留两个数组,一个char数组和一个short数组。在char是两个字节的系统上,MAX_LEN数组将保存short字节,而(MAX_LEN + 1) * 2数组将保存short字节。

如果您一次从两个文件中读取一个字符,并且FILE指针从文件末尾开始读取,则可以将其减少为仅一个short数组。

为了进一步优化,如果您可以承担两次通过文件的开销(考虑到文件缓存,这可能不会太昂贵),则可以对两个字符串进行第一次传递以获取它们的长度。给定两个长度中的较大者,您可以根据执行求和所需的方式分配任意数量的short,这将是两个长度中的较大者加一。另一种选择是在进行操作时realloc您的short数组。

关于c - 在两个文件中添加两个数字,然后将其写入另一个文件。数字太大,不适合整数范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33619910/

相关文章:

c - 无法在 C 中打开文件

python - 从大文件(~2.5GB)读取并存储到 python 列表时出现 MemoryError

amazon-web-services - 在S3存储桶的子文件夹中托管index.html

c - 在 C 中使用 getchar();每次我使用它时它都会移动到下一个字符吗?包括在分配操作中?

c - 如何使用gdb在c中观察字符串

c - C语言写入和读取文件

java - Backspace\b 关于操作系统的 Chacacter 作用域?

ruby - Amazon Linux 最新 ruby

c# - 具有自定义 iOS 负载的 Amazon Simple Notification Service 并不那么简单

c - 为什么 mblen() 总是返回 1?