c - 查找最大和最大负数的程序

标签 c

以下代码打印输入数字的最大值和最大负值(如果我们有 -10 和 -5,则 -5 更大)直到达到一个符号。我的问题是是否有更好的方法来找到最大的负值(避免 INT_MIN 或其他此类笨拙的方法)。

#include <stdio.h>
#include <conio.h>
#include <limits.h>
int main()
{
    float max=0, n, bnn=INT_MIN;
    while(1){
        printf("Enter an integer:");
        if(scanf("%f", &n)!=1){
            break;
        }
        if (n>max)
            max=n;
        else if(n>=bnn)
             bnn=n;
    }
    printf("The maximum number of the entered ones is:%0.2f\n", max);
    printf("The biggest negative number is:%0.2f", bnn);
    getch();
}

最佳答案

从 0.0 开始 bnn

if ((n < 0) && ((bnn == 0.0) || (n > bnn)))
  bnn = n;

关于c - 查找最大和最大负数的程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21005603/

相关文章:

c - 嵌入式python3,没有 "site"模块

c - 数组的动态结构、段错误

c - 指向指针的指针数组删除错误

c - 重绘终端的程序如何工作?

c - 递归循环

c - C编程教程(按问题集)

c - 在不使用数组或动态分配的情况下平滑输入数据

C Union,结构内数组的填充

c - 指向指针的指针有什么作用?字符**

c - 在Linux中以编程方式运行pdftotext xpdf y.text?