假设要找出3个整数a、b、c中的最大值,则可以按照以下步骤进行:

  1. 先比较a和b,将较大的值赋给一个变量max。
  2. 再比较max和c,如果c大于max,则将c的值赋给max。
  3. 最终max就是a、b、c中的最大值。

代码如下:

int findMax(int a, int b, int c) { int max = a; if (compare(b, max)) max = b; if (compare(c, max)) max = c; return max; }

同理,如果要找出3个整数中的最小值,只需要将compare函数的返回值取反即可,代码如下:

int findMin(int a, int b, int c) { int min = a; if (!compare(b, min)) min = b; if (!compare(c, min)) min = c; return min; }

根据选项1—最小值2—最大值找出3个整数中的最小值或最大值通过调用以下函数完成。int compareint x int y	if x y		return 1;	else		return 0;

原文地址: https://www.cveoy.top/t/topic/eefO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录