输入若干行,每行输入两个绝对值不大于100的整数,求这两个整数的和、差、乘积中不超过100的最大数。
时间: 2023-04-24 08:00:42 浏览: 189
题目翻译:给定若干行,每行有两个绝对值不大于100的整数,求这两个整数的和、差、乘积中不超过100的最大数。
解题思路:枚举每一行输入的两个整数,计算它们的和、差、乘积,判断是否小于等于100,如果是,则更新最大值。
代码实现:
max_num = -101
while True:
try:
a, b = map(int, input().split())
for op in ['+', '-', '*']:
if op == '+':
res = a + b
elif op == '-':
res = a - b
else:
res = a * b
if res <= 100 and res > max_num:
max_num = res
except:
break
print(max_num)
相关问题
用c++编译题目,输入若干行,每行输入两个绝对值不大于100的整数,求这两个整数的和、差、乘积中不超过100的最大数。
好的,我会解答这个问题。以下是 C++ 代码:
```c++
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int a, b;
int sum, diff, prod;
int max_value = -101;
while (cin >> a >> b) {
sum = a + b;
diff = abs(a - b);
prod = a * b;
if (sum <= 100 && sum > max_value) {
max_value = sum;
}
if (diff <= 100 && diff > max_value) {
max_value = diff;
}
if (prod <= 100 && prod > max_value) {
max_value = prod;
}
}
cout << max_value << endl;
return 0;
}
```
这个程序中,我们使用了一个 while 循环,不断读入输入的整数对,并计算它们的和、差、乘积中不超过 100 的最大值。
在每次计算后,我们都会判断这个值是否比之前的最大值更大,并在需要时更新最大值。最终,我们输出最大值即可。
希望我的回答能够解决你的问题!
给定两个绝对值不超过100的整数A和b,输出A和b的乘值
好的,给定两个绝对值不超过100的整数A和B,输出A和B的乘积,可以使用以下代码实现:
#include <stdio.h>
int main() {
int a, b;
scanf("%d %d", &a, &b);
printf("%d", a * b);
return 0;
}
以上代码可以读入两个整数A和B,然后输出它们的乘积。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)