简单逻辑问题:在3个数字中找出最大和最小的数字 [英] Simple logic problem: Finding largest and smallest number among 3 numbers

查看:256
本文介绍了简单逻辑问题:在3个数字中找出最大和最小的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个伪代码,以确定3个数字中最小和最大的数字:

I am creating a pseudocode in determining the smallest and largest number among 3 numbers:

我的代码如下:

If (x >= y)  
   largest = x
   Smallest = y
Else 
    largest = y
    Smallest =x

If (z >= largest)
    Largest = z
If (z <= smallest)
    Smallest = z

您认为这是正确的吗?还是有更好的方法来解决这个问题?

Do you think this is correct? or are there better ways to solve this?

推荐答案

假设您有任意数字x, y, z.

伪代码:

largest = x
smallest = x

if (y > largest)  then largest = y
if (z > largest)  then largest = z

if (y < smallest) then smallest = y
if (z < smallest) then smallest = z

如果仅使用变量,赋值,if-else和比较,这是解决问题的一种方法.

This is one way to solve your problem if you're using only variables, assignment, if-else and comparison.

如果您在上面定义了数组和排序操作,则可以使用以下方法:

If you have arrays and a sort operation defined over it, you can use this:

array = [x, y, z]
arrays.sort()
largest  = array[2]
smallest = array[0]


如果具有将数字数组作为参数的maxmin函数,则可以使用以下方法:


If you have a max and min function that takes an array of numbers as an argument, you can use this:

array = [x, y, z]
largest  = max(array)
smallest = min(array)


如果您还使用集合进行位置分配,则可以使用以下方法:


If you also have positional assignment using sets, you can use this:

array = [x, y, z]
(largest, smallest) = (max(array), min(array))


如果您有一个在插入元素时会对其内容进行排序的数据结构,则可以使用以下方法:


If you have a data structure that will sort it's contents when inserting elements, you can use this:

array.insert([x, y, z])
smallest = array[0]
largest = array[2]

这篇关于简单逻辑问题:在3个数字中找出最大和最小的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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