Don't like ads? PRO users don't see any ads ;-)
Guest

Java - tablica dwuwymiarowa max

By: a guest on Jun 25th, 2012  |  syntax: Java  |  size: 1.72 KB  |  hits: 90  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package javaapplication3;
  6.  
  7. import java.util.*;
  8.  
  9. /**
  10.  *
  11.  * @author mike
  12.  */
  13. // MAKSYMALNA TABLICA
  14. public class JavaApplication3 {
  15.  
  16.     public static int[][] tablica;
  17.     public static int wymiar = 20; // wymiar to 20 x 20;
  18.     public static Random generator = new Random(22);
  19.  
  20.     public static int s(int i, int j, int k, int l) {
  21.         int max = Integer.MIN_VALUE;
  22.         for (int firstDimension = i; firstDimension <= j; firstDimension++) {
  23.             for (int secondDimension = k; secondDimension <= l; secondDimension++) {
  24.                 // no i tutaj po prostu wyznaczamy tablicę maksymalną :)
  25.                 // dla kazdego elementu muszę dodać wszystkie następujące elementy
  26.                 int sum = 0;
  27.                 for (int firstDimensionDeeper = firstDimension; firstDimensionDeeper <= j; firstDimensionDeeper++) {
  28.                     for (int secondDimensionDeeper = secondDimension; secondDimensionDeeper <= l; secondDimensionDeeper++) {
  29.                         sum += tablica[firstDimensionDeeper][secondDimensionDeeper];
  30.  
  31.                         if (sum > max) {
  32.                             max = sum;
  33.  
  34.                         }
  35.  
  36.  
  37.                     }
  38.                 }
  39.  
  40.             }
  41.         }
  42.         return max;
  43.     }
  44.  
  45.     public static void main(String[] args) {
  46.         //randomizuje tablice
  47.         tablica = new int[wymiar][wymiar];
  48.  
  49.  
  50.         for (int i = 0; i < wymiar; i++) {
  51.             for (int j = 0; j < wymiar; j++) {
  52.                 tablica[i][j] = generator.nextInt() % 70;
  53.             }
  54.         }
  55.  
  56.  
  57.         System.out.println(s(2, 19, 1, 17));
  58.  
  59.  
  60.     }
  61. }