[英]C - Sum and finding largest/smallest
Could someone take a look at my code and give me some pointers why it isn't working properly. 有人可以看看我的代码并给我一些指示,为什么它不能正常工作。 It is supposed to ask for numbers as long as they're positive integers and then calculate sum of them, divide it by largest and multiply it by smallest number.
只要它们是正整数,就应该要求数字,然后计算它们的总和,将其除以最大数,再乘以最小数。
#include <stdio.h>
int main () {
int n, largest=0, smallest=0;
float sum=0;
scanf("%d", &n);
while (n > 0) {
scanf("%d", &n);
if (n > largest) {
largest = n;
}
if (n < smallest) {
smallest = n;
}
sum += n;
}
sum = sum / largest * smallest;
printf("%f\n", sum);
return 0;
}
Because smallest
starts at zero, it will never change because if (n < smallest)
will never be true. 因为
smallest
从零开始,所以它永远不会改变,因为if (n < smallest)
永远不会为真。 You need: 你需要:
int smallest = INT_MAX;
or similar. 或类似。 For
INT_MAX
you'll need: 对于
INT_MAX
您需要:
#include <limits.h>
at the top. 在顶部。
Move scanf to end of while or else your first input is missed and secondly, you can accept a negative input and do all the processing on it. 将scanf移至一段时间末,否则错过您的第一个输入,其次,您可以接受否定的输入并对其进行所有处理。 Also, smallest has to be the Max value otherwise with
smallest = 0
, n < smallest
will not work 此外,最小必须为最大值,否则
smallest = 0
, n < smallest
将不起作用
#include <stdio.h>
int main () {
int n, largest=0, smallest=INT_MAX;
float sum=0;
scanf("%d", &n);
while (n > 0) {
if (n > largest) {
largest = n;
}
if (n < smallest) {
smallest = n;
}
sum += n;
scanf("%d", &n);
}
sum = sum / largest * smallest;
printf("%f\n", sum);
return 0;
}
Change your while
loop condition from while (n > 0)
to while (scanf("%d", &n) && n > 0)
. 将
while
循环条件从while (n > 0)
更改为while (scanf("%d", &n) && n > 0)
。 In this case you can also remove the first scanf("%d", &n)
and the one in the body of the while
loop. 在这种情况下,您还可以删除第一个
scanf("%d", &n)
和while
循环主体中的那个。
You problem is that because your scanf("%d", &n)
is in the body of your while loop, it is not checked for being greater then 0
when performing if (n > largest)
and if (n < smallest)
. 您的问题是,因为您的
scanf("%d", &n)
在while循环的主体中,所以在执行if (n > largest)
和if (n < smallest)
时,不会检查它是否大于0
。
Also you should initialize your smallest
to the max possible value. 另外,您应该将
smallest
初始化为最大可能值。 In this case being INT_MAX
. 在这种情况下为
INT_MAX
。
Your code fails primarily due to int smallest=0
. 您的代码失败的主要原因是
int smallest=0
。 This can be solve a number of ways. 这可以通过多种方式解决。 I recommend setting the first value read to
largest
and smallest
. 我建议将第一个值设置为“
largest
和“ smallest
。 Also your 2nd scanf("%d", &n);
还有你的第二个
scanf("%d", &n);
s/b at the end of the loop. 循环结束时的s / b。
You have additional problems, though subtle. 尽管存在一些细微问题,但您还有其他问题。
Summing int
into a float
can lose precision in many environments when int
values > about 24,000,000. 总结
int
成float
可以在许多环境中丢失精度int
值>约24,000,000。 Suggest a large integer type to sum your values like uint64_t. 建议使用大整数类型来汇总您的值,例如uint64_t。
The final "divide it by largest and multiply it by smallest number" it the only place floating point math is needed. 最后的“除以最大数并乘以最小数”是唯一需要浮点运算的地方。 The math you did was OK but more likely subject to rounding errors.
您所做的数学还算可以,但是很可能会出现舍入错误。
If your first n
<= 0, you will perform a divide by 0. 如果您的前
n
<= 0,则将被0除。
Sample fix: 修复样本:
#include <stdio.h>
int main() {
int n, largest = 1, smallest = 0; // set largest to 1 to deal with first n being < 0 and thus avoiding /0
uint64_t sum = 0;
scanf("%d", &n);
while (n > 0) {
if (sum > 0) {
if (n > largest) {
largest = n;
}
if (n < smallest) {
smallest = n;
}
} else { // This is the first time as sum is 0
largest = smallest = n;
}
sum += n;
scanf("%d", &n);
}
printf("%lf\n", ((double) sum) / smallest * largest);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.