Pagini recente » Cod sursa (job #2292336) | Cod sursa (job #244643) | Cod sursa (job #3238239) | Cod sursa (job #1363541) | Cod sursa (job #2180071)
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Citire {
public static int make_sum(int[] arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) {
sum += arr[i];
}
return sum;
}
public static void main(String args[]) throws FileNotFoundException {
File text = new File("flip.in");
BufferedWriter writer;
int suma = 0;
int[] rows = new int[16];
int[] cols = new int[16];
int[] temp = new int[16];
Scanner sc = new Scanner(text);
String line = sc.nextLine();
String[] params = line.split("\\s+");
int randuri = Integer.valueOf(params[0]);
int coloane = Integer.valueOf(params[1]);
int[][] matrix = new int[randuri][coloane];
int t = 0;
while (sc.hasNextLine()) {
line = sc.nextLine();
String[] vals = line.split("\\s+");
for (int j = 0; j < coloane; j++) {
matrix[t][j] = Integer.valueOf(vals[j]);
}
t++;
}
for (int i = 0; i < randuri; i++) {
for (int j = 0; j < coloane; j++) {
rows[j] = matrix[i][j];
temp[j] = matrix[i][j] * -1;
}
if (make_sum(rows) < make_sum(temp)) {
for (int p = 0; p < coloane; p++) {
matrix[i][p] = temp[p];
}
} else {
for (int p = 0; p < coloane; p++) {
matrix[i][p] = rows[p];
}
}
}
for (int i = 0; i < coloane; i++) {
for (int j = 0; j < randuri; j++) {
cols[j] = matrix[j][i];
temp[j] = matrix[j][i] * -1;
}
if (make_sum(cols) < make_sum(temp)) {
for (int p = 0; p < randuri; p++) {
matrix[p][i] = temp[p];
}
} else {
for (int p = 0; p < randuri; p++) {
matrix[p][i] = cols[p];
}
}
}
for (int i = 0; i < randuri; i++) {
for (int j = 0; j < coloane; j++) {
suma += matrix[i][j];
}
}
try {
writer = new BufferedWriter(new FileWriter("flip.out"));
writer.write(String.valueOf(suma));
writer.close();
} catch (IOException ex) {
Logger.getLogger(Citire.class.getName()).log(Level.SEVERE, null, ex);
}
}
}