Advertisement
LoraOrliGeo

MDA5

Sep 6th, 2019
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. public class Task5 {
  2. public static void main(String[] args) {
  3.  
  4. int[][] matrix = {
  5. {1, 2, 3, 4},
  6. {5, 6, 7, 8},
  7. {9, 10, 11, 12},
  8. {13, 14, 15, 16},
  9. };
  10.  
  11. int maxRowSum = 0;
  12. int maxColSum = 0;
  13.  
  14. for (int row = 0; row < matrix.length; row++) {
  15. int currSumRow = 0;
  16. for (int col = 0; col < matrix[row].length; col++) {
  17. currSumRow += matrix[row][col];
  18. }
  19.  
  20. if (currSumRow >= maxRowSum || row == 0) {
  21. maxRowSum = currSumRow;
  22. }
  23. }
  24.  
  25. for (int col = 0; col < matrix[0].length; col++) {
  26. int currColSum = 0;
  27. for (int row = 0; row < matrix.length; row++) {
  28. currColSum += matrix[row][col];
  29. }
  30.  
  31. if (currColSum >= maxColSum) {
  32. maxColSum = currColSum;
  33. }
  34. }
  35.  
  36. System.out.println("Max row sum = " + maxRowSum);
  37. System.out.println("Max col sum = " + maxColSum);
  38.  
  39. if (maxRowSum > maxColSum) {
  40. System.out.println("Max row sum > max col sum");
  41. } else if (maxColSum > maxRowSum) {
  42. System.out.println("Max col sum > max row sum");
  43. } else {
  44. System.out.println("Max row sum = max col sum");
  45. }
  46.  
  47. }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement