Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Seance15Nov2019 {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- int a = 123 ;
- int b = 1230;
- int m;
- m = max2numbers(a, b);
- System.out.println("le maximum est " + m);
- int c = 46;
- m = max3numbers (a, b, c);
- System.out.println("le maximum des 3 entier est " + m);
- // Tableau
- int[] tableau = {1, 4, 6}; // Tableau d'entier avec 3 éléments
- System.out.println(tableau);
- System.out.println(tabToString(tableau));
- System.out.println(tabToString2(tableau));
- System.out.println(maximumInArray(tableau));
- System.out.println("whereInArray " + whereInArray(6, tableau));
- int[] tableau2 = {-10, -4, 7, -3, 2, 1, 12, 4, -1};
- int z = plusProcheDeZero(tableau2);
- System.out.println("l'entier le plus proche de zero : " + z);
- int[] tableauSort = { -10, -2, 2, 3, 20};
- int[] tableauUnSort = {-2, 1, 6, 5};
- System.out.println("Le tableau 1 est rangé : " + isSort(tableauSort) + " / le tableau2 aussi : " + isSort(tableauUnSort));
- }
- /**
- * max2number retourne le maximum de n1 et n2
- * @param n1
- * @param n2
- * @return
- */
- static int max2numbers (int n1, int n2)
- {
- int m;
- if(n1 > n2) {
- m = n1;
- }
- else
- {
- m = n2;
- }
- return m;
- }
- /**
- * max3numbers retourne le maximum entre 3 entiers
- * @param n1
- * @param n2
- * @param n3
- * @return
- */
- static int max3numbers (int n1, int n2, int n3)
- {
- return(max2numbers(n1, max2numbers(n2, n3)));
- }
- static String tabToString(int[] T)
- {
- String s = "[" ;
- int i = 0 ; // Compteur pour parcourir les indices de T
- while(i < T.length-1)
- {
- s += T[i] + ", ";
- i++;
- }
- s += T[T.length-1] + "]";
- return s ;
- }
- static String tabToString2(int[] T)
- {
- String s = "[" ;
- for (int i=0; i < T.length-1; i++)
- {
- s += T[i] + ", "; // équivaut à s = s + T[i] + ","
- }
- s += T[T.length-1] + "]";
- return s;
- }
- /**
- *
- * @param T
- * @return
- */
- static int maximumInArray(int[] T)
- {
- int m;
- m = T[0];
- for(int i=1; i<T.length; i++)
- {
- if(T[i] > m)
- {
- m=T[i];
- }
- }
- return m;
- }
- /**
- * Recherche une valeur dans un tableau
- * @param value la valeur cherchée
- * @param T le tableau
- * @return index of T is value is in T, -1 otherwise
- */
- static int whereInArray(int value, int[] T)
- {
- int iValue = -1; // Si la valeur n'est pas dans le tableau
- int i = 0;
- while(iValue == -1 && i<T.length)
- {
- if(T[i]==value)
- {
- iValue = i ;
- }
- i++;
- }
- return iValue;
- }
- static int plusProcheDeZero(int[] T)
- {
- int i = 0;
- int r = 0;
- while(i < T.length)
- {
- if(r==0 || Math.abs(r) > Math.abs(T[i]))
- {
- r = T[i];
- }
- i++;
- }
- return r;
- }
- //CORRECTION
- static int plusProcheDeZeroCorrect(int[] T)
- {
- int min; // Distance minimale à zero
- int iMin; //Indice de la valeur pour la distance minimale à zéro
- min = Math.abs(T[0]); // Distance minimale pour le premier
- iMin = 0; //elément du tableau
- //Parcours du tableau T pour tester les autres éléments
- for(int i=1; i<T.length; i++)
- {
- if(Math.abs(T[i]) < min) //nouveau plus proche de zero
- {
- min = Math.abs(T[i]); //Remplacer min et iMin par
- iMin = i; //Les valeurs pour i
- }
- }
- return T[iMin];
- }
- static boolean isSort(int[] T)
- {
- int i = 1;
- int r = T[0];
- while(i < T.length)
- {
- r = T[i-1];
- if(r > T[i])
- {
- return false;
- }
- i++;
- }
- return true;
- }
- //CORRECTION
- static boolean estCroissant(int[] T)
- {
- boolean croissant = true;
- int i = 0;
- while(croissant && i<T.length+1)
- {
- croissant = (T[i] < T[i+1]);
- i++;
- }
- return croissant;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement