1 Άσκηση 1 η Δίνεται το παρακάτω απόσπασμα προγράμματος που εμφανίζει την ακολουθία των αριθμών Fibonacci. Εξ ορισμού, οι πρώτοι δύο αριθμοί Fibonacci είναι το 0 και το 1, και κάθε επόμενος αριθμός είναι το άθροισμα των δύο προηγούμενων. με και Το πρόγραμμα αρχικά διαβάζει το πλήθος των όρων της ακολουθίας Fibonacci και στη συνέχεια εμφανίζει τους όρους αυτούς: public class Fibonacci extends Program{ public void run(){ int count = readint("dwste to plithos twn orwn tis akolouthias Fibοnacci: "); if(count>=0) System.out.println(0); if(count>=1) System.out.println(1); if (count>=2) { for (i=2; i<count; i++){ public class Fibonacci extends Program{ public void run(){ int count = readint("dwste to plithos twn orwn tis akolouthias fib: "); if(count>=0) System.out.println(0); //fib(0)=0; if(count>=1) System.out.println(1); //fib(1)=1; if(count>=2) { //fib(n)=fib(n-1)+fib(n-2); for(i=2; i<count; i++){ currentfib = fib1 + fib2; //fib(n)=fib(n-1)+fib(n-2); System.out.println(currentFib); fib2=fib1; //για fib(n+1) το fib'(n-2) = fib(n-1) fib1=currentfib; //για fib(n+1) το fib'(n-1) = fib(n)
2 Άσκηση 2 η Γράψτε ένα πρόγραμμα το οποίο διαβάζει έναν ακέραιο και ελέγχει αν ανήκει (αποτελεί όρο) στην ακολουθία Fibonacci. public class FibonacciCheck extends Program{ int n = readint("poios arithmos thelete na elegxthei an anhkei sthn akolouthia Fibonacci? "); boolean flag=false; if ((n==0) (n == 1)) { System.out.println("To "+n+" anhkei"); flag = true; if(n>=2) { if (!flag) println("o arithmos " + n + " den periexetai");
3 public class FibonacciCheck extends Program{ int n = readint("poios arithmos thelete na elegxthei an anhkei sthn akolouthia Fibonacci? "); boolean flag=false; if ((n==0) (n == 1)) { System.out.println("To "+n+" anhkei"); flag = true; if(n>=2) { while(true) { currentfib = fib1 + fib2; println(currentfib); if (n == currentfib) { println ("To "+n+" anhkei"); flag = true; fib2=fib1; fib1=currentfib; if (currentfib>=n) break; if (!flag) println("o arithmos " + n + " den periexetai");
4 Άσκηση 3 η Το παρακάτω πρόγραμμα υπολογίζει το πλήθος των συνδυασμών (combinations) n ανά κ: όπου n factorial (n!) = 1 * 2 * * n Συμπληρώστε με τις κατάλληλες εντολές το σώμα των συναρτήσεων combinations και factorial public class Combinations extends ConsoleProgram { int n = readint("enter number of objects in the set (n): "); int k = readint("enter number to be chosen (k): "); println("c(" + n + ", " + k + ") = " + combinations(n, k)); * Returns the mathematical combinations function C(n, k), * which is the number of ways of selecting k objects * from a set of n distinct objects. int combinations(int n, int k) { * Returns the factorial of n, which is defined as the * product of all integers from 1 up to n. int factorial(int n) {
* File: Combinations.java * ----------------------- * This program computes the mathematical combinations function * C(n, k), which is the number of ways of selecting k objects * from a set of n distinct objects. public class Combinations extends ConsoleProgram { int n = readint("enter number of objects in the set (n): "); int k = readint("enter number to be chosen (k): "); println("c(" + n + ", " + k + ") = " + combinations(n, k)); * Returns the mathematical combinations function C(n, k), * which is the number of ways of selecting k objects * from a set of n distinct objects. private int combinations(int n, int k) { return factorial(n) / (factorial(k) * factorial(n - k)); * Returns the factorial of n, which is defined as the * product of all integers from 1 up to n. private int factorial(int n) { int result = 1; for (int i = 1; i <= n; i++) { result *= i; return result; 5
6 Άσκηση 4 η Το παρακάτω πρόγραμμα δημιουργεί ένα τετράγωνο (50x50 pixels) και το μετακινεί κάνοντας 1000 χρονικά βήματα από την επάνω αριστερή γωνία του παραθύρου μέχρι την κάτω δεξιά. * File: AnimatedSquare.java * ------------------------- * This program animates a square so that it moves from the * upper left corner of the window to the lower right corner. import acm.graphics.*; public class AnimatedSquare extends GraphicsProgram { // Δημιούργησε το τετράγωνο square ως ένα αντικείμενο τύπου GRect // Γέμισε (setfilled) το τετράγωνο (όχι μόνο περίγραμμα) // Τοποθέτησε (add) το τετράγωνο στο παράθυρο γραφικών // Υπολόγισε την απόσταση μετακίνησης του τετραγώνου dx και dy ως // πηλίκο του πλάτους του παραθύρου γραφικών δια του πλήθους των // χρονικών βημάτων double dx = ; double dy = ; Για κάθε βήμα { // Μετακίνησε το τετράγωνο (square.move) κατά dx kai dy // Κάνε παύση (pause) για 20 χιλιοστά του δευτερολέπτου Private constants private static final int N_STEPS = ; private static final int PAUSE_TIME = ; private static final double SQUARE_SIZE = ;
7 * File: AnimatedSquare.java * ------------------------- * This program animates a square so that it moves from the * upper left corner of the window to the lower right corner. import acm.graphics.*; public class AnimatedSquare extends GraphicsProgram { GRect square = new GRect(0, 0, SQUARE_SIZE, SQUARE_SIZE); square.setfilled(true); add(square); double dx = (getwidth() - SQUARE_SIZE) / N_STEPS; double dy = (getheight() - SQUARE_SIZE) / N_STEPS; for (int i = 0; i < N_STEPS; i++) { square.move(dx, dy); pause(pause_time); Private constants private static final int N_STEPS = 1000; private static final int PAUSE_TIME = 20; private static final double SQUARE_SIZE = 50;