Cod sursa(job #1054682)

Utilizator clau05Claudiu Avram clau05 Data 14 decembrie 2013 08:35:15
Problema Jocul Flip Scor 20
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.91 kb
#include <fstream>
#include <iostream>
#include <math.h>
using namespace std;

int matrixSum(int** matrix, int lines, int columns);
void printMatrix(int** matrix, int lines, int columns);

int main() {
	int columns, lines;
	int** matrix;
	ifstream inFile;
	inFile.open("flip.in", std::ios_base::in);
	inFile >> lines >> columns;
	matrix = new int*[lines];
	for(int line = 0; line < lines; line++) {
		matrix[line] = new int[columns];
		for(int column = 0; column < columns; column++) {
			inFile >> matrix[line][column];
		}
	}
	inFile.close();

	printMatrix(matrix, lines, columns);

	for(int l = 0; l < lines; l++) {
		int pozitives = 0, negatives = 0;
		for(int c = 0; c < columns; c++) {
			if(matrix[l][c] > 0) {
				pozitives += matrix[l][c];
			} else {
				negatives -= matrix[l][c];
			}
		}
		
		if(negatives > pozitives) {
			for(int c = 0; c < columns; c++) {
				matrix[l][c] = -matrix[l][c];
			}
		}
	}

	for(int c = 0; c < columns; c++) {
		int pozitives = 0, negatives = 0;
		for(int l = 0; l < lines; l++) {
			if(matrix[l][c] > 0) {
				pozitives += matrix[l][c];
			} else {
				negatives -= matrix[l][c];
			}
		}
		if(negatives > pozitives) {
			for(int l = 0; l < lines; l++) {
				matrix[l][c] = -matrix[l][c];
			}
		}
	}
	printMatrix(matrix, lines, columns);

	int sum = matrixSum(matrix, lines, columns);
	cout << "Suma maxima este: " << sum;
	ofstream outFile;
	outFile.open("flip.out", std::ios_base::out);
	outFile << sum;
	outFile.close();
}

void printMatrix(int** matrix, int lines, int columns) {
	for(int l = 0; l < lines; l++) {
		for(int c = 0; c < columns; c++) {
			cout << matrix[l][c] << ' ';
		}
		cout << endl;
	}
	cout << endl;
}

int matrixSum(int** matrix, int lines, int columns) {
	int rezult = 0;
	for (int l = 0; l < lines; l++) {
		for (int c = 0; c < columns; c++) {
			rezult += matrix[l][c];
		}
	}
	return rezult;
}