You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

编写程序:单变量接收5个值,无数组无循环,用if else求最大最小值

Solution: Find Max/Min of 5 Values Without Arrays or Loops

Got it, let's break this down. The problem constraints are pretty specific: no arrays, no loops, just one variable to receive 5 values, and we can only use if-else statements to find the maximum and minimum. Here's a practical way to implement this:

Step 1: Start with the first value

First, we'll take the first input and use it to initialize our max and min variables—since it's the only value we have right now, it's both the largest and smallest. We'll reuse a single variable (let's call it current) for all subsequent inputs.

Step 2: Process each value one by one with if-else

For each of the remaining 4 values, we'll overwrite current with the new input, then compare it against our current max and min. We update max if the new value is larger, and update min if it's smaller (using nested if-else as needed).

Example Code (Python)

# Capture first value and initialize max/min
current = float(input("Enter value 1: "))
max_val = current
min_val = current

# Process second value
current = float(input("Enter value 2: "))
if current > max_val:
    max_val = current
else:
    if current < min_val:
        min_val = current

# Process third value
current = float(input("Enter value 3: "))
if current > max_val:
    max_val = current
else:
    if current < min_val:
        min_val = current

# Process fourth value
current = float(input("Enter value 4: "))
if current > max_val:
    max_val = current
else:
    if current < min_val:
        min_val = current

# Process fifth value
current = float(input("Enter value 5: "))
if current > max_val:
    max_val = current
else:
    if current < min_val:
        min_val = current

# Print results
print(f"Maximum value: {max_val}")
print(f"Minimum value: {min_val}")

How this works:

  • We don't use any loops—each input is handled in a separate block of code, reusing the current variable every time.
  • No arrays are involved; we only track two extra variables (max_val and min_val) to keep our running extremes.
  • Each comparison uses simple if-else logic to check if the new value beats the current max or min, updating accordingly.

If you're working in a different language like C, the logic stays the same—you'd just use scanf instead of input() and adjust the syntax slightly.

内容的提问来源于stack exchange,提问作者Farhan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:12:53