【问题标题】:Printing sum of digits greater than average打印大于平均值的数字总和
【发布时间】:2019-09-19 18:41:47
【问题描述】:

对于作业,我必须用 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;
}

希望有人可以帮助我!

【问题讨论】:

  • 你可能会得到一些反对意见,因为它的格式看起来像是一个家庭作业转储。尝试将其浓缩为一个特定的问题。您已经从不起作用的测试用例中获得了良好的开端。
  • 我会用fgets 读取输入,然后你的(ASCII)数字就在一个不错的数组中。

标签: c sum average variable-assignment


【解决方案1】:

除了未能在i++; 之前初始化i = 0;,您最大的问题是您会被大量不必要的和难以描述的变量名弄糊涂。 ext, ext2, tot, big, big2, avg, rem, rem2 只是可变沙拉。

虽然您需要ext2 来保存您减去%(模)的变量的副本以找到totavg,但不需要rem2big2(您可以简单地重用rem),而您只需要一个big(假设这是您的变量,其中包含大于avg的数字总和)

此外,您找到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&lt;2000000000

最后,虽然您可以使用数字输入并使用模来提取每个数字,但将输入读取为字符串然后简单地遍历每个字符减去 '0' 以获得数值(如 Weather Vane 在 cmets 中所建议的)

编辑整理变量

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

#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 -&gt; 18777 -&gt; 0778 -&gt; 8。如果您还有其他问题,请查看并告诉我。

【讨论】:

  • 我去看看。这些可能需要特殊情况。个位数,平均值是一个数字,所以没有什么可添加的。例如5avg = 5/1,然后是大于5 的数字总和——没有数字大于5。就像我说的那样,让我看看,我会删除另一条评论或编辑我的答案。
  • @BramKoop 为什么你说777 结果应该是7?使用7+7+7 = 21 时,平均值为21 / 3 = 7 - 没有比avg 更高的数字相加。你是说如果所有数字都相等,那么即使没有数字大于平均值,你还有另一个特殊条件,当多个数字都相同时输出一个数字??
  • 糟糕,我的意思不是 7。愚蠢的......它确实应该是 0,因为没有比平均值更大的数字。你是对的。
  • 好的,那我可以取出多余的变量了...这将是一个疯狂的要求,但随后的分配充满了疯狂的要求:)
  • 哈哈。真的。我将尝试删除和重命名一些变量,并编写一行将 777 的结果设置为 0。到目前为止,谢谢!
【解决方案2】:

您忘记将变量 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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多