最大价值 [英] Largest Value

查看:100
本文介绍了最大价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

提示用户从标准输入中输入三个整数,使用scanf()读取三个整数,并在标准输出上显示最大数量的
三.
注意:
·用户提示应为请输入三个整数:"
·输出格式为"a,b和c的最大值是m",后跟换行符,其中a,b和c是用户输入的三个整数,m是
中的最大值 他们.

Prompt the user to enter three integers from the standard input, read the three integers using scanf(), and display on the standard output the largest number of the
three.
Note:
· User prompt should be "Please enter three integers: "
· The output format is "The largest value of a, b and c is m" followed by a newline, where a, b and c are the three integers inputted by the user and m is the largest of
them.

A sample run of your program is:
Please enter three integers: 23 -9 182
The largest value of 23, -9 and 182 is 182
where "23 -9 182" are user input. Note that the second output line ends with a newline so that the
cursor is positioned to the beginning of the next line on the screen after the program finishes running.




我的程序:
它适用于大多数情况,但是当我输入0 -900 1
它给了我0作为最大值.为什么呢?





My program:
It works with most cases but when i enter 0 -900 1
it gives me 0 as the largest value. why is that?


#include <stdio.h>
int main() 
{
    int a, b, c, m;
 
    printf("Please enter three integers: ");
    scanf("%d %d %d", &a, &b, &c);
    m = (a>b) ? a : b && (a>c) ? a : c && (b>c) ? b : c;
    
    printf("The largest value of %d, %d and %d is %d\n", a, b, c, m);
    
    return 0;
}

推荐答案

可能您需要以下内容:
m =(a> b)? ((a> c)?a:c):((b> c)?b:c);
May be you need something as follows:
m = (a > b) ? ((a > c) ? a : c ) : ((b > c) ? b : c);


为什么不
if (a > b)
{
  if( a > c )
    m = a;
  else
    m = c;
}
else
{
  if (b > c)
    m = b;
  else
    m = c;
}



我的意思是,如果三元运算符不是必需的...
:rolleyes:



I mean, if the ternary operator isn''t mandatory...
:rolleyes:


如果要使用三元运算符,有一个窍门,
If you want to use ternary operator there is a trick,
#define Max(a,b) ((a>b)?a:b)

#include <stdio.h>
int main()
{
    int a, b, c, m;
    printf("Please enter three integers: ");
    scanf("%d %d %d", &a, &b, &c);
    m = Max(Max(a,b),c);
    printf("The largest value of %d, %d and %d is %d\n", a, b, c, m);
    return 0;
}



您可以递归使用此宏.



You can use this macro recursively.


这篇关于最大价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆