ITI0011:maxAxisSum

Allikas: Kursused
Redaktsioon seisuga 22. september 2014, kell 03:51 kasutajalt Ago (arutelu | kaastöö)
(erin) ←Vanem redaktsioon | Viimane redaktsiooni (erin) | Uuem redaktsioon→ (erin)
Mine navigeerimisribale Mine otsikasti

Koodinäited tunniülesannete kohta.

Neljapäev kell 8

maxRowSum leiab 3x3 tabelist maksimaalse summa, mis ühe rea elemendid kokku annavad. maxRowSumGeneral teeb sama asja, aga NxN tabeli peal.

Neljapäev kell 14

maxColSum leiab 3x3 tabelist maksimaalse summa, mis ühe veeru elemendid kokku annavad. maxColSumGeneral teeb sama asja, aga NxN tabeli peal.

Kood

main funktsioon, mis testib kõiki allpool kirjeldatud funktsioone.

<source lang="java"> public class N8 {

public static void main(String[] args) { System.out.println(maxRowSum(new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9})); System.out.println(maxRowSumGeneral(new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16})); System.out.println(maxColSum(new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9})); System.out.println(maxColSumGeneral(new int[]{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}));

} </source>

maxRowSum

<source lang="java"> /** * Find the maximum sum of row elements in * a 3x3 table which is represented as one-dimensional * array. * We assume that every element in the table is * a non-negative number, so the sum is also non-negative. * @param numbers One-dimensional array which represents 3x3 table. * @return The maximum sum in a single row. */ public static int maxRowSum(int[] numbers) { int maxSum = -1; for (int i = 0; i < 3; i++) { // let's loop over the rows

// i * 3 => first element in i-th row // i * 3 + 1 => second element // i * 3 + 2 => third element int sum = numbers[i * 3] + numbers[i * 3 + 1] + numbers[i * 3 + 2]; if (sum > maxSum) { maxSum = sum; } } return maxSum; }

/** * A general method to find the maximum sum of a * single row. * @param numbers One-dimensional array which represents NxN table. * @return The maximum sum of one row * @see N8#maxRowSum(int[]) */ public static int maxRowSumGeneral(int[] numbers) { // let's assume we have a square int size = (int) Math.sqrt(numbers.length); if (size * size != numbers.length) { System.out.println("Not a square!"); return -1; } // let's mark -1 as the max sum so far // (ideally it could be -infinity) int maxSum = -1; for (int row = 0; row < size; row++) { // let's loop over all the row int sum = 0; // initial sum, we will add values here for (int elInRow = 0; elInRow < size; elInRow++) { // let's loop over all the elements in the row

// let's sum up every element in a row // (first element in the row has index row * size) sum += numbers[row * size + elInRow]; } if (sum > maxSum) { // if the current sum is better (bigger) // than previous max, then let's store // the current sum as the max. maxSum = sum; } } return maxSum; } </source>

maxColSum

<source lang="java"> /** * Finds the sum of column elements which * has the highest sum on a 3x3 table which * is represented by one-dimensional array. * @param numbers One-dimensional array which represented 3x3 table. * @return The maximum column sum */ public static int maxColSum(int[] numbers) { int maxSum = -1; for (int i = 0; i < 3; i++) { int sum = numbers[i] + numbers[i + 3] + numbers[i + 6]; if (sum > maxSum) { maxSum = sum; } } return maxSum; }

/** * Finds the sum of column elements which * has the highest sum on a square table represented * by one-dimensional array. * @param numbers One-dimensional array which represents NxN table * @return The maximum sum */ public static int maxColSumGeneral(int[] numbers) { // let's assume we have a square int size = (int) Math.sqrt(numbers.length); if (size * size != numbers.length) { System.out.println("Not a square!"); return -1; } // let's mark -1 as the max sum so far // (ideally it could be -infinity) int maxSum = -1; for (int col = 0; col < size; col++) { // let's loop over all the row int sum = 0; // initial sum, we will add element values here for (int elInCol = 0; elInCol < size; elInCol++) { // let's loop over all the elements in the column

// let's sum up every element in a column // (first element in the column has index elInCol * size) sum += numbers[elInCol * size + col]; } if (sum > maxSum) { // if the current sum is better (bigger) // than previous max, then let's store // the current sum as the max. maxSum = sum; } } return maxSum; } }

</source>