题解 | #最大最小值#
最大最小值
https://ac.nowcoder.com/acm/problem/22015
这里主要是用到了一个公式:
求最大值: return (a + b + abs(a-b))/2
#include <iostream> #include <cmath> using namespace std; int max(int a,int b) { return (a + b + abs(a-b))/2; } int min(int a,int b) { int x = (a + b + abs(a-b))/2; if(x == a) return b; else return a; } int main() { int x,y,z; cin >> x >> y >> z; cout << "The maximum number is : "<< max(x,max(y,z)) << endl; cout << "The minimum number is : "<< min(x,min(y,z)) << endl; return 0; }