打印大于平均值的数字总和

Printing sum of digits greater than average

对于一项作业,我必须在 C 中编写一个代码 来打印大于其平均值的数字的数字。

我已经写了一些代码,但是当我插入单个数字或具有相同数字的数字(如 555)时,它没有给我答案。在 555 --> 5(和 8 -> 8、77 -> 等)的情况下,它应该给出

我写的代码是:

(我对每一步都进行了评论。)

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

int main (int argc, char *v[])  {
    /* imput: 1 number between 1 and 2.000.000.000 */
    int n;          /*  stores the value                            */
    int ext, ext2;  /*  stores a copy of the number                 */
    int rem;        /*  stores the remainders                       */
    int tot;        /*  stores the total of the digits              */
    int avg;        /*  stores the average of the digits            */
    int i;          /*  counts the amount of digits                 */
    int big;        /*  stores the biggest and second biggest value */

    scanf("%d", &n);

    /* seperate digits and add them */
    ext=n;
    ext2=n;
    tot=0;
    big=0;
    avg=0;
    i=0;

    while(ext!=0 && n<2000000000 && n>=1){
        rem=ext%10;
        tot=tot+rem;
        ext=ext/10;
        i++;
    };  

    /* average value */
    avg=tot/i;      

    /* find and print higher numbers */
    while (ext2>0){
        rem = ext2 % 10;
        if (rem > avg){
            big += rem;
        };
        ext2 = ext2 / 10;
    }

    if(n<100 && n>9){
        printf("%d\n", big);
    } else if(big==avg){
        printf("%d\n", 0);
    } else if(big>=avg){
        printf("%d\n", big);
    };

    return 0;
}

希望有人能帮助我!

您忘记将变量 i 初始化为 0。我已经更正了您的代码,它给出了预期的结果。单个数字的答案是 0,因为一个数字的平均值始终是数字本身,因此数字永远不会大于平均值。

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

int main ()
{
    /* imput: 1 number between 1 and 2.000.000.000 */
    int n;          /*  stores the value                            */
    int ext, ext2;  /*  stores a copy of the number                 */
    int rem, rem2;  /*  stores the remainders                       */
    long tot;       /*  stores the total of the digits              */
    int avg;        /*  stores the average of the digits            */
    int i;          /*  counts the amount of digits                 */
    int big, big2;  /*  stores the biggest and second biggest value */
    int sum;        /*  stores sum of digits greater than average   */
    scanf("%d", &n);

    /* seperate digits and add them */
    ext=n;
    ext2=n;
    tot=0;
    big=0;
    big2=0;
    avg=0;
    i=0;
    sum=0;

    while(ext!=0 && n<2000000000 && n>=1)
    {
        rem=ext%10;
        tot=tot+rem;
        ext=ext/10;
        i++;
    };

    /* average value */
    avg=tot/i;

    /* find and print higher numbers */

    while(ext2>0)
    {
        rem2=ext2%10;
        if(rem2>avg)
        {
            sum+=rem2;
        };
        ext2=ext2/10;
    };

    printf("sum of digits greater than average: %d",sum);

    return 0;
}

除了无法在 i++; 之前初始化 i = 0; 之外,您最大的问题是您将自己与大量不必要的和难以描述的变量名称混淆了。 ext, ext2, tot, big, big2, avg, rem, rem2只是变色拉。

虽然您需要 ext2 来保存您减少 %(取模)以找到 totavg 的变量的副本,但没有必要对于 rem2big2(您可以简单地重复使用 rem)并且您只需要一个 big(假设您的变量持有大于 [=18= 的数字总和) ])

此外,查找 big(并使用 big2)的循环比实际需要的要混乱得多。你不关心rem2是否大于big2然后设置big2 = rem2;你关心的是这个数字是否大于avg。 (就是这样)您只需要:

    /* find and print higher numbers */
    while (ext2>0){
        rem = ext2 % 10;
        if (rem > avg){
            big += rem;
        };
        ext2 = ext2 / 10;
    }

现在 big 保留大于平均值的数字总和。

重用变量和使用描述性变量名将大大有助于保持逻辑清晰并防止不必要的变量蔓延。编写函数时要牢记一个正常的经验法则(始终牢记在心是件好事),但如果您正在编写函数并且发现自己使用了超过 4 个变量,则可能需要重构你的程序。 (这不是硬性规定,但关键是要防止你掉进的变量沙拉)

为什么long tot;?最多 1+9+9+9+9+9+9+9+9+9 = 82int 的使用就像您对其余变量的使用一样很好。 (考虑到您的约束 1≤n<2000000000

最后,虽然您可以按原样获取数字输入并使用模数提取每个数字,但将输入读取为字符串然后简单地迭代每个字符减去 '0' 获取数值(如 Weather Vane 评论中所建议)

编辑整理变量

根据我们的持续讨论,我决定放弃快速编辑,稍微整理一下其余代码并减少变量的数量,并在声明变量时初始化变量。你可以做类似的事情:

#include <stdio.h>

int main (void)  {

    int n,          /*  stores the value                                */
        tmp,        /*  stores a copy of the number                     */
        rem,        /*  stores the remainders                           */
        tot = 0,    /*  stores the total of the digits                  */
        avg = 0,    /*  stores the average of the digits                */
        digits = 0, /*  counts the number of digits                     */
        big = 0;    /*  stores the sum of digits larger than avg        */

    /* imput: 1 number (1 <= n && n <= 2.000.000.000    */
    if (scanf("%d", &n) != 1) { /* read/validate number */
        fputs ("error: invalid integer input.\n", stderr);
        return 1;
    }
    if (n < 1 || 2000000000 < n) {  /* validate n in allowable range */
        fputs ("error: input outside allowable value.\n", stderr);
        return 1;
    }

    tmp = n;
    while (tmp) {           /* sum digits for average */
        rem = tmp % 10;
        tot += rem;
        tmp /= 10;
        digits++;
    }
    avg = tot / digits;     /* calculate average */

    tmp = n;
    while (tmp) {           /* sum numbers higher than avg */
        rem = tmp % 10;
        if (rem > avg)
            big += rem;
        tmp /= 10;
    }

    printf ("sum of digits greater than avg : %d\n", big);
}

没什么特别的,但是 989 -> 18777 -> 0778 -> 8。检查一下,如果您还有其他问题,请告诉我。