Pagini recente » Cod sursa (job #3345201) | Cod sursa (job #3351920) | Cod sursa (job #3333963) | Monitorul de evaluare | Cod sursa (job #3338422)
#include <fstream>
#include <iostream>
using namespace std;
ifstream f("flip.in");
ofstream g("flip.out");
long a[17][17], b[17], k = 0;
int m, n;
long sum_row(int x)
{
long s = 0;
for (int i = 1; i <= n; i++)
{
s += a[x][i];
}
return s;
}
long sum_col(int x)
{
long s = 0;
for (int i = 1; i <= m; i++)
{
s += a[i][x];
}
return s;
}
void maneta_row(int x)
{
for (int i = 1; i <= n; i++)
{
a[x][i] *= -1;
}
}
void maneta_col(int x)
{
for (int i = 1; i <= m; i++)
{
a[i][x] *= -1;
}
}
int main()
{
f >> m >> n;
for (int i = 1; i <= m; i++)
for (int j = 1; j <= n; j++)
f >> a[i][j];
int gasit = 1;
while (gasit == 1)
{
gasit = 0;
for (int i = 1; i <= m; i++)
{
int ss = sum_row(i);
if (ss < -1 * ss)
{
maneta_row(i);
gasit = 1;
}
}
for (int i = 1; i <= n; i++)
{
int ss = sum_col(i);
if (ss <= -1 * ss)
{
maneta_col(i);
gasit = 1;
}
}
}
for (int i = 1; i <= m; i++)
{
for (int j = 1; j <= n; j++)
{
k += a[i][j];
//cout << a[i][j]<<' ';
}
//cout << '\n';
}
g << k;
return 0;
}