Pagini recente » Cod sursa (job #2594731) | Cod sursa (job #855500) | Cod sursa (job #485828) | Cod sursa (job #2949936) | Cod sursa (job #119189)
Cod sursa(job #119189)
#include <fstream>
using std::ifstream; using std::ofstream;
const unsigned int MAX_MAT_SIZE=16;
struct Mat {
long mat[MAX_MAT_SIZE][MAX_MAT_SIZE];
unsigned int size_l,size_c;
};
bool invColoane (Mat &M);
bool invLinii (Mat &M);
long long Suma (const Mat &M);
int main (void)
{
ifstream f_in("flip.in");
ofstream f_out("flip.out");
if (!f_in.good() || !f_out.good()) {
f_in.close(); f_out.close();
return -1;
}
Mat M;
f_in>>M.size_l>>M.size_c;
for (unsigned int l=0; l<M.size_l; l++)
for (unsigned int c=0; c<M.size_c; c++)
f_in>>M.mat[l][c];
bool coloana=true,a1=true,a2=true;
while (a1||a2) {
if (coloana) a1=invColoane(M);
else a2=invLinii(M);
coloana=!coloana;
}
f_out << Suma(M) << '\n';
return 0;
}
//true daca a fost inversata vreo coloana
bool invColoane (Mat &M)
{
bool inv=false;
long S;
unsigned int l;
for (unsigned int c=0; c<M.size_c; c++) {
S=0;
for (l=0; l<M.size_l; l++)
S += M.mat[l][c];
if (S<0) {
inv=true;
for (l=0; l<M.size_l; l++)
M.mat[l][c] = ~M.mat[l][c]+1;
}
}
return inv;
}
bool invLinii (Mat &M)
{
bool inv=false;
long S;
unsigned int c;
for (unsigned int l=0; l<M.size_l; l++) {
S=0;
for (c=0; c<M.size_c; c++)
S += M.mat[l][c];
if (S<0) {
inv=true;
for (c=0; c<M.size_c; c++)
M.mat[l][c] = ~M.mat[l][c]+1;
}
}
return inv;
}
long long Suma (const Mat &M)
{
long long ret=0;
for (unsigned int c=0; c<M.size_c; c++)
for (unsigned int l=0; l<M.size_l; l++)
ret += M.mat[l][c];
return ret;
}