C++ 编程题:求解 maxx(x,y)/maxx(x+y,x*y) 的值
以下是用 C++ 编写的解题程序:
#include <iostream>
using namespace std;
int maxx(int x, int y) {
return (x > y) ? x : y;
}
double calculate(int x, int y) {
int max1 = maxx(x, y);
int max2 = maxx(x + y, x * y);
return static_cast<double>(max1) / max2;
}
int main() {
int x, y;
cin >> x >> y;
double result = calculate(x, y);
cout << result << endl;
return 0;
}
该程序首先定义了一个函数 maxx,用于返回两个整数中较大的一个数。然后定义了另一个函数 calculate,用于计算 maxx(x,y)/maxx(x+y,x*y) 的结果。在 main 函数中,首先从标准输入中读取两个整数 x 和 y,然后调用 calculate 函数计算结果,并将结果输出到标准输出中。
注意,为了确保结果为小数,我们在返回结果时使用了 static_cast<double> 将结果转换为 double 类型。
原文地址: https://www.cveoy.top/t/topic/qcAK 著作权归作者所有。请勿转载和采集!