Rispondi a: esercizi sull array

Blog Forum Java esercizi sull array Rispondi a: esercizi sull array

#5575
ANTONIO PAGANO
Ospite
Up
0
Down
::

Buongiorno,
una possibile soluzione è la seguente:

	public static void main(String[] args) {
		// int[] tmp = {1, 2, -2, 3, 4, 5, 6};
		// int[] tmp = {1, 2, 3, 4, 5, 6};
		int[] tmp = { 1, 2, -3, 4, 5, 6 };

		boolean trovato = false;
		for (int i = 0; i < tmp.length; i++) {
			int curr_sum = 0;

			// try all subarrays starting with 'i'
			for (int j = i; j < tmp.length; j++) {
				curr_sum = curr_sum + tmp[j];

				if (curr_sum == 0) {
					trovato = true;

				}

			}
		}
		System.out.print("Il suddetto array contiene un sottoarray con 0 sum: " + trovato);

	}