三元运算符?:求三个数的最大值

代码:

#include <stdio.h>
#include <stdlib.h>

int max(int, int, int);

int main(void) {

	int a, b, c;

	a = 100, b = 200, c = 300;
	printf("a = %d, b = %d, c = %d, max = %d\n", a, b, c, max(a, b, c));

	a = 200, b = 300, c = 100;
	printf("a = %d, b = %d, c = %d, max = %d\n", a, b, c, max(a, b, c));

	a = 300, b = 100, c = 200;
	printf("a = %d, b = %d, c = %d, max = %d\n", a, b, c, max(a, b, c));

	return EXIT_SUCCESS;
}

int max(int a, int b, int c) {
	return a > b ? (a > c ? a : c) : (b > c ? b : c);
}
时间: 2024-08-05 23:22:01

三元运算符?:求三个数的最大值的相关文章

php 写程序求三个数的最大值

最简单的调用PHP自带的max函数即可:echo max(1,2,3,4,5); 如果要自定义函数的话:function test($a,$b,$c){ return $a > $b ?($a > $c ? $a : $c) : ($b > $c ? $b :$c);}

Java基础——使用三元运算符判断一个数的奇偶性

要求: 使用三元运算符判断一个数的奇偶性 实现代码: /** * 使用三元运算符判断用户输入的一个数的奇偶性 */ import java.util.Scanner; public class Odd_even { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请输入一个整数:"); long num1 = input.nextLo

C#编写代码:求三个数中的最大数

static void Main(string[] args)        {            float x, y, z, temp;            Console.Write("请输入一个实数:");            x = float.Parse(Console .ReadLine() );            Console.Write("请输入一个实数:");            y = float.Parse(Console .

JAVA_新建一个方法并且求三个数中的最大值

package wac.wev.as;//新建一个方法在求最大值import java.util.Scanner; public class MaxLian {public static void main(String[] args){//键盘录入以及导包Scanner sc= new Scanner(System.in);//数据接收System.out.println("请输入第一个数据:");int a = sc.nextInt();System.out.println(&qu

求三个数中的最大值

三个数a b c int a = 10; int b = 100; int c = 1000; 第一种方式if嵌套 int max = 0; if (a > b) { if (a > c) { max = a; } else { max = c; } } else if (b > a) { if (b > c) { max = b; } else { max = c; } } else if (c > a) { if (c > b) { max = c; } else

C#实战--三个数找最大值(4种方法)

using System; using System.Collections.Generic; using System.Linq; using System.Text; /* * 编一个程序 * 从键盘上输入三个数 * 用三元运算符(? :)把最大数找出来. */ namespace PI { class Program { static void Main(string[] args) { Int32 var_a, var_b, var_c,max; var_a = int.Parse(Co

【c语言】利用指针求三个数的最大数和最小数

比较费空间的笨方法: #include<stdio.h>void main(){    int i,j,k,*m,*n,*q,temp;    printf("请输入三个数:");    scanf("%d,%d,%d",&i,&j,&k);    printf("三个数是:%d,%d,%d\n",i,j,k);    m=&i,n=&j,q=&k;    if(*n<*m){  

if语句求三个数中最大的

Console.WriteLine("请输入第一个数:"); int a = Convert.ToInt32( Console.ReadLine()); Console.WriteLine("请输入第二个数:"); int b = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入第三个数:"); int c = Convert.ToInt32(Console.ReadLine(

C++求两个数的最大值

//不使用if,:?等判断语句,求两个数字中最大的那个数字. #include<iostream> using namespace std; int main() { int a = -10; int b = -100; int c = (a + b + abs(a - b))/2; //abs(x)是求绝对值的函数,a+b+(a与b的差值)就是最大数的两倍,再除以2即为最大数. cout << c << endl; return 0; } #include <i