C 小的数不是小数Problem ID 7346Contest ID 5092必做题Time Limit1000msMemory Limit524288kb题目描述大哈现在有4个整数他想经过一些操作使他最后手里只剩1个数。但他才1岁最近刚自学了加法和乘法。所以他决定通过加法和乘法来进行操作。因为他还没有学会加括号所以4个整数他打算选用3个符号来计算。他想每次选两个数然后用给定符号的顺序对这两个数
解题思路: 通过观察可以发现,无论怎么进行加法和乘法操作,最后剩下的数都是4个数的和或者积。因此,只需要计算给定的4个数的和和积,然后比较两个结果的大小,取最小值即可。
具体步骤如下:
- 读取输入的四个整数a, b, c, d和三个符号op1, op2, op3。
- 计算四个数的和sum和积product。
- 根据op1, op2, op3的顺序进行运算,计算出剩下的数result。
- 将sum和product与result比较,取较小值作为最终答案。
- 输出最终答案。
伪代码如下:
read a, b, c, d
read op1, op2, op3
sum = a + b + c + d
product = a * b * c * d
if op1 == '+' and op2 == '+' and op3 == '+':
result = sum + sum + sum
else if op1 == '*' and op2 == '*' and op3 == '*':
result = product * product * product
else if op1 == '+' and op2 == '+' and op3 == '*':
result = sum + sum * sum
else if op1 == '+' and op2 == '*' and op3 == '+':
result = sum + product + sum
else if op1 == '*' and op2 == '+' and op3 == '+':
result = product + sum + sum
else if op1 == '*' and op2 == '*' and op3 == '+':
result = product * product + sum
else if op1 == '*' and op2 == '+' and op3 == '*':
result = product + sum * product
else if op1 == '+' and op2 == '*' and op3 == '*':
result = sum * product * product
min_value = min(sum, product, result)
print min_value
时间复杂度分析: 该算法的时间复杂度为O(1),因为只进行了有限次的计算和比较操作
原文地址: https://www.cveoy.top/t/topic/h314 著作权归作者所有。请勿转载和采集!