gpt4 book ai didi

c - 打印数字总和大于平均值

转载 作者:行者123 更新时间:2023-11-30 16:13:32 26 4
gpt4 key购买 nike

对于作业,我必须用 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;之前i++; ,您最大的问题是您对大量不必要且不伦不类的变量名称感到困惑。 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 = 82 。使用int就像您对其余变量使用的那样就可以了。 (考虑到您的限制 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 -> 18 ,和777 -> 0778 -> 8 。检查一下,如果您还有其他问题,请告诉我。

关于c - 打印数字总和大于平均值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58017228/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com