Advertisement
MonsterScripter

Triangle de Pascal

Nov 19th, 2019
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. import iut.algo.Clavier;
  2.  
  3. /** Class Le triangle de Pascal
  4.   * @author Monster
  5.   * date : 18/11/2019
  6.   */
  7.  
  8. public class TP6_TrianglePascal
  9. {
  10.     public static String Affichage (int[][] tab)
  11.     {
  12.         String retour;
  13.         retour = "";
  14.         for (int i=0;i<tab.length;i++)
  15.         {
  16.             retour = retour + i + ":";
  17.             for(int j=0;j<tab[0].length;j++)
  18.             {
  19.                 retour = retour + " ";
  20.                 if(tab[i][j]<10){retour = retour + " ";}
  21.                 if(tab[i][j] == 0){retour = retour + "  ";}
  22.                 else {retour = retour + tab[i][j] + " ";}
  23.             }
  24.             retour = retour + "\n";
  25.         }
  26.         return retour;
  27.     }
  28.     public static void main(String[] a)
  29.     {
  30.         /*----------------------*/
  31.         /*  Données             */
  32.         /*----------------------*/
  33.         final int TAILLE = 9;
  34.  
  35.         int[][]  tabPascal;
  36.         tabPascal = new int[TAILLE][TAILLE];
  37.        
  38.         int numLign=0, numCol=0;
  39.  
  40.  
  41.         /*----------------------*/
  42.         /* Remplir le tableau   */
  43.         /*----------------------*/
  44.         for (int i=0; i<tabPascal.length; i++)
  45.         {
  46.             for (int j=0; j<tabPascal.length; j++)
  47.             {
  48.                 tabPascal[i][j] = 0;
  49.             }
  50.         }
  51.  
  52.         /*----------------------*/
  53.         /*  Instructions        */
  54.         /*----------------------*/
  55.         tabPascal[0][0]=1;
  56.  
  57.         for (int i=1; i<tabPascal.length; i++)
  58.         {
  59.             for (int j=0; j<tabPascal.length; j++)
  60.             {
  61.                 if (j==0)
  62.                 {
  63.                     tabPascal[i][j] = tabPascal[i-1][j];
  64.                 }
  65.                 else
  66.                 {
  67.                     tabPascal[i][j] = tabPascal[i-1][j-1]+tabPascal[i-1][j];
  68.                 }
  69.             }
  70.         }
  71.            
  72.            
  73.         /*----------------------*/
  74.         /*      Affichage       */
  75.         /*----------------------*/
  76.         System.out.println(Affichage(tabPascal));
  77.            
  78.  
  79.     }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement