C++ 实现整数序列最小修改代价 - 算法题解
#include
int main() {
int n;
cin >> n;
vector
int minCost = INT_MAX;
for (int x = -1000; x <= 1000; x++) {
int cost = 0;
for (int i = 0; i < n; i++) {
cost += pow(a[i] - x, 2);
}
minCost = min(minCost, cost);
}
cout << minCost << endl;
return 0;
}
原文地址: https://www.cveoy.top/t/topic/pXxX 著作权归作者所有。请勿转载和采集!