一道编程题,最好用C++语言实现,谢谢大神
可以用next_permutation函数实现全排列就比较简单啦...我电脑上粗写试了一下可以,你看一下,有啥看不懂再讨论哈。
#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
int maxScore(int* arr, int len) {
int max = INTMAX_MIN;
int sum = 0;
sort(arr, arr + len);
do
{
for (int i = 0; i < len - 1; i++) {
sum = sum + arr[i] ^ arr[i + 1];
}
if (max < sum)
max = sum;
sum = 0;
} while (next_permutation(arr, arr + len));
return max;
}
int main() {
int count;
cin >> count;
int* arr = new int[count];
for (int i = 0; i < count; ++i) {
cin >> arr[i];
}
cout << maxScore(arr, count);
return 0;
}