Pagini recente » Cod sursa (job #122920) | Cod sursa (job #3346824) | Cod sursa (job #570417) | Cod sursa (job #2210376) | Cod sursa (job #3326998)
#include <fstream>
using namespace std;
void readMatrix(int n, int m, ifstream &fin, long arr[][100])
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
fin >> arr[i][j];
}
}
}
void calculateColumnSums(int n, int m, long arr[][100], long sumColumns[])
{
for (int j = 0; j < m; j++)
{
long sum = 0;
for (int i = 0; i < n; i++)
{
sum += arr[i][j];
}
sumColumns[j] = sum;
}
}
void calculateRowSums(int n, int m, long arr[][100], long sumRows[])
{
for (int i = 0; i < n; i++)
{
long sum = 0;
for (int j = 0; j < m; j++)
{
sum += arr[i][j];
}
sumRows[i] = sum;
}
}
void calculateSumOfMatrix(int n, int m, long arr[][100], long &totalSum)
{
totalSum = 0;
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
totalSum += arr[i][j];
}
}
}
void flipColumns(int n, int m, long arr[][100], long sumColumns[])
{
for (int j = 0; j < m; j++)
{
if (sumColumns[j] < 0)
{
for (int i = 0; i < n; i++)
{
arr[i][j] = -arr[i][j];
}
}
}
}
void flipRows(int n, int m, long arr[][100], long sumRows[])
{
for (int i = 0; i < n; i++)
{
if (sumRows[i] < 0)
{
for (int j = 0; j < m; j++)
{
arr[i][j] = -arr[i][j];
}
}
}
}
int main()
{
ifstream fin("flip.in");
ofstream fout("flip.out");
int n, m;
fin >> n >> m;
long arr[100][100];
long sumColumns[100] = { 0 };
long sumRows[100] = { 0 };
readMatrix(n, m, fin, arr);
calculateColumnSums(n, m, arr, sumColumns);
flipColumns(n, m, arr, sumColumns);
calculateRowSums(n, m, arr, sumRows);
flipRows(n, m, arr, sumRows);
long totalSum;
calculateSumOfMatrix(n, m, arr, totalSum);
fout << totalSum << endl;
}