Pagini recente » Cod sursa (job #767384) | Borderou de evaluare (job #1711719) | Cod sursa (job #2012482) | Cod sursa (job #3302860) | Cod sursa (job #3315048)
#include <iostream>
#include <fstream>
#include <cassert>
#include <cstdlib>
#include <algorithm>
#include <cstring>
int arr_sum(int *const arr, const int rows, const int cols) {
int sum{0};
for(int i = 0; i < rows; ++i) {
for(int j = 0; j < cols; ++j) {
sum += arr[i * cols + j];
}
}
return sum;
}
void flip(int *const arr, const int rows, const int cols, const int row, const int col)
{
for(int i = 0; i < rows; ++i) {
arr[i * cols + col] *= -1;
}
for(int i = 0; i < cols; ++i) {
arr[row * cols + i] *= -1;
}
}
int main()
{
std::fstream f("flip.in");
assert(f.is_open());
int rows, cols;
f >> rows >> cols;
assert(rows >= 1);
assert(cols <= 16);
int *arr = (int*)malloc(sizeof(int) * rows * cols);
memset(arr, 0, sizeof(int)*rows*cols);
for(int i = 0; i < rows * cols; ++i) {
f >> arr[i];
}
f.close();
int biggest = arr_sum(arr, rows, cols);
for(int i = 0; i < rows; ++i) {
for(int j = 0; j < cols; ++j) {
flip(arr, rows, cols, i, j);
int b = arr_sum(arr, rows, cols);
if(b > biggest) {
biggest = b;
}
flip(arr, rows, cols, i, j);
}
}
std::ofstream o("flip.out");
o << biggest;
o.close();
return 0;
}