用 C 打印位置数字系统 [英] Printing a positional numeric system in C

查看:44
本文介绍了用 C 打印位置数字系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个函数来打印具有给定基数和位数的位置数字系统.例如,Base = 2 和 nDigits = 3,输出必须是这样的:

I'm trying to write a function that print a positional numeric system with given base and number of digits. For example, with Base = 2 and nDigits = 3 the output have to be like this:

000
001
010
011
...
111

现在,我尝试做一些事情,但我只是失败了.考虑到我无法存储数字,我只需要打印它们,这就是我使用支持"数组的动态分配的原因.到目前为止,这就是我试图做的,显然没有做它的意图......我认为唯一正确的部分是打印 Base^nDigits 的所有组合.(b=base,n=nDigits).

Now, I've tried to do something but I've only failed. Consider that I can't store the numbers, I just have to print them, that's why I use a dynamic allocation of a 'support' array. That's what I tried to do so far, obviously not doing what it's intended to... I think the only correct part is to print all the combinations for Base^nDigits. (b=base, n=nDigits).

void printNumeration(int b, int n){
    int i, j=0;
    int *array = calloc(n, sizeof(int));

    if (array == NULL){
        printf("Allocation failed.\n");
        exit(0);
    }


    for (i=0; i<pow(b, n); i++){
        for (j=0; j<n; j++){
            printf("%d", array[j]);
            array[j]++;
            if (array[j] == n){
                array[j] = 0;
            }
        }
        printf("\n");
    }
}

如果我的完全错误,您也可以提供一些关于更好解决方案的提示.

You can also give some tips about a better solution if mine is completely wrong.

推荐答案

首先,不要将 pow 用于整数. - 至少不要无需四舍五入.pow 使用不精确的浮点算法进行求幂.上周有一个问题出现错误,因为 pow(10, 2) 是 99.9999999... 截断为 int 是 99.

First of all, Do not use pow for integer numbers. - at least not without rounding. pow uses an inexact floating point algorithm to do the exponentiation. There was just last week a question where there was an error because pow(10, 2) was 99.9999999... which truncated to int was 99.

也就是说,很可能存在一个平台,其中 pow(2, 3) 如您的示例所示,结果为 7.999999...;由于仅通过截断小数将双精度数转换为整数,这意味着您的代码运行 7 个循环而不是 8 个!double 比较也是如此;8.0 仍然大于 7.999999999999999.因此我们使用 round 来确保得到的数字是正确四舍五入为最接近的整数值(本例中为 8.0).

That is, there might well be a platform, where pow(2, 3) as in your example results in 7.999999...; as doubles are converted to integers by just truncating the decimals, this would mean that your code runs 7 loops instead of 8! The same would be still true for double comparison; 8.0 is still greater than 7.999999999999999. Thus we use round to ensure that the resulting number is rounded correctly to the nearest integer value (8.0 in this case).

此外,您还需要预先计算这个数字,而不是每次循环迭代.

Also you'd want to count this number beforehand, not for every loop iteration.

我们有 2 个内部循环.首先打印数字,然后向后打印 - 如果 kth 位数字等于 b,我们将其设置为 0,我们将 k 减 1,现在增加 k第一个数字,然后重复.

We have 2 inner loops. First prints the numbers and second works backwards - if the kth digit is equal to b we set it to 0, we decrease k by 1 and increase now kth digit by one, and repeat.

最后记得释放calloc分配的内存.

Finally, remember to free the memory allocated by calloc.

void printNumeration(int b, int n) {
    int *digits = calloc(sizeof(int), n);
    int max = round(pow(b, n));
    for (int i = 0; i < max; i ++) {
        for (int j = 0; j < n; j ++) {
            printf("%d", digits[j]);
        }
        int k = n - 1;
        digits[k] ++;
        while (k && digits[k] == b) {
            digits[k] = 0;
            k--;
            digits[k] ++;
        }
        printf("\n");
    }
    free(digits);
}

这篇关于用 C 打印位置数字系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆