解一: 先运用sort函数对四个参数进行大小的升序排序 再然后将最小最大 次小次大进行相加 最后运用abs函数求出最大值
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
int main (){
int a = 0,b = 0, c =0, d = 0 ,A_rating = 0 ,B_rating = 0;
cin >> a>>b>>c>>d;
int nums[4] = {a,b,c,d};
sort(nums,nums+4);
int sums = abs((nums[0]+nums[3])- (nums[1]+nums[2]));
cout <<sums;
return 0;
}
同上 但是运用了vector容器
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int main() {
int a, b, c, d;
cin >> a >> b >> c >> d;
vector<int> nums = {a, b, c, d};
sort(nums.begin(), nums.end());
int team1 = nums[0] + nums[3];
int team2 = nums[1] + nums[2];
int result = abs(team1 - team2);
cout << result << endl;
return 0;
}
解二 : 先将问题进行分析 发现组队方式仅有C42除以2 =3种组队方式
即为对三种方式分别用abs算出三种各自的绝对差 再通过min函数筛选出差值里面的最小值