All pastes #279322 Raw Edit

Stuff

public text v1 · immutable
·published 2006-12-14 23:27 UTC
rendered paste body
public class Muutos {
	
	public static void main(String[] args) {
		int[] t = {1,2,3,-1,-2,-3,-3,2,2};
		int[] c = annaMuutossuunnat(t);
		System.out.println(annaPisinMuutos(c));
	}

	/**
	 * Palauttaa muutostaulukon, johon on sijoitettu arvot periaattella:
	 * m[i] = -1, jos t[i] > t[i+1]
	 * m[i] =  0, jos t[i] == t[i+1]
	 * m[i] =  1, jos t[i] < t[i+1]
	 *
	 * AE: t != null
	 */
	public static int[] annaMuutossuunnat(int[] t) {
		int[] m = new int[t.length-1];
		for (int i = 0; i < m.length; i++) {
			if (t[i] > t[i+1])
				m[i] = -1;
			if (t[i] == t[i+1])
				m[i] = 0;
			if (t[i] < t[i+1])
				m[i] = 1;
		}
		return m;
	}

	/**
	 * Laskee pisimmän yhtäjaksoisen muutosjakson pituuden.
	 *
	 * AE: m != null
	 */
	public static int annaPisinMuutos(int[] m) {
		int pisin = 0;
		int apu = 1;
		for (int i = 1; i < m.length; i++) {
			if (m[i] == m[i-1])
				apu = apu + 1;
			else {
				if (pisin < apu) {
					pisin = apu;
				}
				apu = 1;
			}
		}
		return pisin;
	}
}