tp recursivite

This commit is contained in:
Simoes Lukas
2025-10-09 12:11:25 +02:00
parent 56258c01e6
commit 91d20e0ba6
15 changed files with 181 additions and 0 deletions

Binary file not shown.

View File

@@ -0,0 +1,17 @@
public class Factorielle {
public static int factorielle(int n, int indentation) {
for (int i = 0; i != indentation; i++) {
System.out.print(" ");
}
System.out.println("Valeur de n : " + n);
if (n == 1) {
return 1;
}
for (int i = 0; i != indentation; i++) {
System.out.print(" ");
}
System.out.println("Valeur renvoyée : " + n + factorielle(n - 1, indentation++));
return n * factorielle(n - 1, indentation++);
}
}

Binary file not shown.

View File

@@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {
System.out.println(Integer.parseInt(args[0]) + "! = " + Factorielle.factorielle(Integer.parseInt(args[0]), 0));
}
}

Binary file not shown.

View File

@@ -0,0 +1,17 @@
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
Tableaux t = new Tableaux(args.length);
int[] entiers = t.convertitTab(args, args.length);
t.afficheTab(entiers, 0);
System.out.println("Nombre d'entiers pairs : " + t.entiersPairs(0));
System.out.println("Valeur maximale : " + t.maximum(0, 0));
t.afficheTabInverse(entiers, entiers.length-1);
}
}

Binary file not shown.

View File

@@ -0,0 +1,61 @@
public class Tableaux {
private int[] tableau;
public Tableaux(int tailleTab) {
this.tableau = new int[tailleTab];
}
public int[] convertitTab(String[] tab, int tailleTab) {
if (tailleTab == 1) {
this.tableau[0] = Integer.parseInt(tab[0]);
return this.tableau;
}
this.tableau[tailleTab-1] = Integer.parseInt(tab[tailleTab-1]);
return this.convertitTab(tab, --tailleTab);
}
public void afficheTab(int[] tab, int increment) {
if (increment == tab.length-1) {
System.out.println(tab[increment]);
} else {
System.out.print(tab[increment] + " | ");
afficheTab(tab, ++increment);
}
}
public int entiersPairs(int increment) {
if (increment == this.tableau.length) {
return 0;
}
if (this.tableau[increment] % 2 == 0) {
return 1 + entiersPairs(++increment);
} else {
return 0 + entiersPairs(++increment);
}
}
public int maximum(int increment, int maxActuel) {
if (increment == this.tableau.length-1) {
if (this.tableau[increment] > maxActuel) {
return this.tableau[increment];
}
return maxActuel;
}
if (this.tableau[increment] > maxActuel) {
return this.maximum(increment+1, this.tableau[increment]);
}
return this.maximum(increment+1, maxActuel);
}
public void afficheTabInverse(int[] tab, int decrement) {
if (decrement == 0) {
System.out.println(tab[0]);
} else {
System.out.print(tab[decrement] + " | ");
afficheTabInverse(tab, --decrement);
}
}
}

Binary file not shown.

View File

@@ -0,0 +1,27 @@
public class Fibonacci {
public static int fibonacci(int n, int indentation) {
for (int i = 0; i != indentation; i++) {
System.out.print(" ");
}
System.out.println("Valeur de n : " + n);
if (n == 0) {
for (int i = 0; i != indentation; i++) {
System.out.print(" ");
}
System.out.println("Valeur de retour : " + 0);
return 0;
} if (n == 1) {
for (int i = 0; i != indentation; i++) {
System.out.print(" ");
}
System.out.println("Valeur de retour : " + 1);
return 1;
}
for (int i = 0; i != indentation; i++) {
System.out.print(" ");
}
System.out.println("Valeur de retour : " + fibonacci(n-2, ++indentation) + fibonacci(n-1, ++indentation));
return fibonacci(n-2, ++indentation) + fibonacci(n-1, ++indentation);
}
}

Binary file not shown.

View File

@@ -0,0 +1,5 @@
public class Main {
public static void main(String[] args) {
System.out.println(Fibonacci.fibonacci(Integer.parseInt(args[0]), 0));
}
}

View File

@@ -0,0 +1,20 @@
import java.awt.*;
import javax.swing.*;
public class Fenetre extends JFrame {
private int ordre;
public Fenetre(int ordre) {
this.ordre = ordre;
this.setSize(500, 500);
this.setLocation(100, 100);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLayout(new GridLayout(1, 1));
this.add(new JFlocon(ordre));
}
}

View File

@@ -0,0 +1,23 @@
import java.awt.*;
import javax.swing.*;
public class JFlocon extends JComponent {
private int ordre;
public JFlocon(int ordre) {
this.ordre = ordre;
}
@Override
public void paintComponent(Graphics pinceau) {
Graphics secondPinceau = pinceau.create();
if (this.isOpaque()) {
this.setColor(this.getBackgroundColor());
this.fillRect(0, 0, this.getWidth(), this.getHeight());
}
}
}

View File

@@ -0,0 +1,6 @@
public class Main {
public static void main(String[] args) {
Fenetre fenetre = new Fenetre(Integer.parseInt(args[0]));
fenetre.setVisible(true);
}
}