Tags:
create new tag
view all tags

Metodologie di Programmazione a.a. 2008-2009
(canale E-O)

Soluzioni 18 marzo 2009

Esercizi [Piramidi] e [Piramidi_capovolte]
import static java.lang.System.*;

public class CharPyramid {
    private int left, top, height;

    public CharPyramid(int l, int t, int h) {
        left = l;
        top = t;
        height = h;
    }

    public void draw() {
        for (int i = 0 ; i < top ; i++) out.println();
        int s = left + height - 1, w = 1;
        for (int i = 0 ; i < height ; i++) {
            for (int j = 0 ; j < s ; j++) out.print(' ');
            for (int j = 0 ; j < w ; j++) out.print('*');
            out.println();
            s--;
            w += 2;
        }
    }

    public void drawUpsideDown() {
        for (int i = 0 ; i < top ; i++) out.println();
        int s = left, w = 2*height - 1;
        for (int i = 0 ; i < height ; i++) {
            for (int j = 0 ; j < s ; j++) out.print(' ');
            for (int j = 0 ; j < w ; j++) out.print('*');
            out.println();
            s++;
            w -= 2;
        }
    }
}

Esercizio [Duplicati]
import static java.lang.System.*;
import java.util.*;

public class Duplicati {
        // ritorna true se x appare nel sottoarray v[i...j]
    public static boolean trova(int[] v, int i, int j, int x) {
        while (i <= j && v[i] != x) i++;
        return (i <= j ? true : false);
    }
    public static void main(String[] args) {
        Scanner input = new Scanner(in);
        out.print("Digita un numero: ");
        int n = input.nextInt();
        out.print("Digita "+n+" numeri: ");
        int[] v = new int[n];
        for (int i = 0 ; i < n ; i++)
            v[i] = input.nextInt();
        boolean dup = false;
        for (int i = 0 ; i < n ; i++) {
            int x = v[i];
            if (!trova(v, 0, i-1, x) && trova(v, i+1, n-1, x)) {
                out.println(x);
                dup = true;
            }
        }
        if (!dup) out.println("non ci sono duplicati");
    }
}

Esercizio [Costellazioni_di_caratteri]
// in un file CharPoint.java
public class CharPoint {
    public final int x, y;
    public final char ch;

    public CharPoint(char c, int x, int y) {
        ch = c;
        this.x = x;
        this.y = y;
    }
}

// in un file CharPointSet.java
import static java.lang.System.*;

public class CharPointSet {
    private CharPoint[] set;

    public CharPointSet() {
        set = new CharPoint[0];
    }

    public void add(CharPoint p) {
        int n = set.length;
        CharPoint[] newSet = new CharPoint[n + 1];
        for (int i = 0 ; i < n ; i++) newSet[i] = set[i];
        set = newSet;
        set[n] = p;
    }

    public void draw() {
        int cx = -1, cy = 0;
        for (int i = 0 ; i < set.length ; i++) {
            int mx = -1, my = -1, ind = 0;
            for (int j = 0 ; j < set.length ; j++) {
                int x = set[j].x, y = set[j].y;
                if (y > cy || (y == cy && x > cx))
                    if (mx < 0 || (y < my || (y == my && x < mx))) {
                        ind = j;
                        my = y;
                        mx = x;
                    }
            }
            if (my > cy) cx = -1;
            for (int j = cy ; j < my ; j++) out.println();
            for (int j = cx ; j < mx ; j++) out.print(' ');
            out.print(set[ind].ch);
            cx = mx;
            cy = my;
        }
    }
}




-- RiccardoSilvestri - 22 Mar 2009

Topic revision: r1 - 2009-03-22 - RiccardoSilvestri






 
Questo sito usa cookies, usandolo ne accettate la presenza. (CookiePolicy)
Torna al Dipartimento di Informatica
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2022 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback