QBasic Trouver le triangle a la plus grande S

Résolu
Salut a tous.

Voilà le problème.
On donne un nombre de points dans un repaire cartésien A, B, C, D...
Il faut trouver la surface de tous les triangles possible calculer la surface et dire le quelle est le plus grand on affichant ces coordonner et sa surfsce.

REM triangle
INPUT "n="; n
IF n < 3 THEN
    PRINT "avec le nombre de point que vous avez donne vous ne pouvez pas avoir de triangle"
ELSE
    'TRIANGLE = prob(n)
    'PRINT "nous avons une probabilité de "; TRIANGLE; "triangle avec "; n; " de points."
 
END IF
 
FUNCTION g_point ()
m = 2
RANDOMIZE TIMER
DIM a(n, m)
FOR i = 1 TO n
    FOR j = 1 TO m
        a(i, j) = (RND * 10)    ////génère une table de point avec des nombre aléatoire 
    NEXT j
NEXT i
END FUNCTION
 
'FUNCTION CalculAire (Ax, Ay, Bx, By, Cx, Cy)
'CalculAire = ABS((Ax * (By - Cy) + Bx * (Cy - Ay) + Cx * (Ay - By)) / 2)      /////trouve la surface avec ses coordonne.
'CalculAire = 1 / 2 * ABS((Ax * Cy) - (Ax * By) + (Bx * Ay) - (Bx * Cy) + (Cx * By) - (Cx * Ay))
'END FUNCTION
 
FUNCTION prob (n)
F = 1
G = 6
H = 1
l = n - 3
FOR i = 1 TO n                       ///////////// calque le nombre des triangles possible 
    F = F * i
NEXT i
FOR i = 1 TO l
    H = H * i
NEXT i
prob = F / (H * G)
END FUNCTION


Mon plus grand problème est de faire une table genre
ABC
ABD
ACD
BCD

Pas ABC ACB CBA car c'est la même chose

A D C
|x,y | x,y| x,y |aire1

Puis de comparer le résultat avec la fonction max et afficher le les coordonne est le résultat de la surface maximal.

INPUT "max elements"; n
DIM A(n)
max = A(1)
FOR i = 1 TO n
    IF A(i) > max THEN max = A(i)
NEXT i
PRINT "Max="; max
END


si vous avez d'autre idée n'hésiter pas
merci.

6 réponses

  1. Bonjour,

    Ce petit programme te construit une tables de triangles
    En faisant varier n, tu verras s'afficher les sommets des triangles.

    Tu peux t'en inspirer pour la suite.

    DECLARE SUB sommets (n AS INTEGER)
    
    DIM SHARED triangle AS INTEGER
    DIM SHARED tableTriangles(1000, 3) AS INTEGER
    CLS
    sommets (4)
    PRINT
    PRINT "Nombre de triangles : " ; STR$(triangle)
    END
    
    '
    SUB sommets (n AS INTEGER)
    DIM i AS INTEGER, j AS INTEGER, k AS INTEGER
    
    triangle = 0
    
    FOR i = 1 TO n - 2
    FOR j = i + 1 TO n - 1
    FOR k = j + 1 TO n
    
    triangle = triangle + 1
    tableTriangles(triangle, 1) = i
    tableTriangles(triangle, 2) = j
    tableTriangles(triangle, 3) = k
    
    PRINT STR$(i); "  "; STR$(j); "  "; STR$(k)
    
    NEXT k
    NEXT j
    NEXT i
    
    END SUB
    0
    1. salut.
      dite comment je peux générée çà ?
      si j'ai 
      a
      b
      c
      
      abc
      ------------------------
      si j'ai
      
      a
      b
      c
      d
      
      abc
      abd
      acd
      bcd
      
      ------------------------
      si j'ai
      
      a
      b
      c
      d
      e
      
      abc
      abd
      abe
      acd
      ace
      ade
      bcd
      bce
      bde
      cde
      
      .
      .
      .
      ext
      

      merci.
      0
      1. Salut.

        Donc voilà, pour ne pas dire que j'ai ne rien fais et que j'attente de vous une repense toute cuite.

        CLS
        REM triqngle
        OPTION BASE 1 'Tout les tableaux commence par 1 et non 0
        INPUT "n="; n
        RANDOMIZE TIMER
        DIM a(n, 2)
        
        PRINT
        PRINT "------------------------Generateur coordonnées-------------------------------"
        PRINT
        
        FOR i = 1 TO n
            FOR j = 1 TO 2
                a(i, j) = INT(RND * 50)
                PRINT a(i, j),
            NEXT j
            PRINT
        NEXT i
        
        PRINT
        PRINT "---------------Calcule de la longueur des cotes de triangle--------------------"
        PRINT
        
        PRINT w
        w = probligne(n)
        longligne = 0
        DIM ligne(w, 5)
        m = 0
        FOR i = 1 TO n - 1
            FOR j = i + 1 TO n
                m = m + 1
                ligne(m, 1) = a(i, 1)
                ligne(m, 2) = a(i, 2)
                ligne(m, 3) = a(j, 1)
                ligne(m, 4) = a(j, 2)
                longligne = SQR((ligne(m, 3) - ligne(m, 1)) ^ 2 + (ligne(m, 4) - ligne(m, 2)) ^ 2)
                ligne(m, 5) = longligne
                PRINT ligne(m, 1), ligne(m, 2), ligne(m, 3), ligne(m, 4), ligne(m, 5)
            NEXT j
        NEXT i
        
        
        PRINT
        PRINT "--------------------Calcule de la surface  de triangle -----------------------"
        PRINT
        
        't = prob
        DIM triangle(100, 4)
        DIM o AS INTEGER, p AS INTEGER, q AS INTEGER
        h = 0
        z = 0
        FOR o = 1 TO n - 2
            
            FOR p = o TO n - 1
               
                FOR q = p + 1 TO n
                    h = h + 1
                   
                    PRINT ligne(o, 5), ligne(p, 5), ligne(q, 5)
        
                    'triangle(h, 1) = ligne(o, 5)
                    'triangle(h, 2) = ligne(p, 5)
                    'triangle(h, 3) = ligne(q, 5)
                    'z = ((triangle(h, 1) + triangle(h, 2) + triangle(h, 3)) / 2)
                    'PRINT z
                    'striangle = SQR(z * (z - triangle(h, 1)) * (z - triangle(h, 2)) * (z - triangle(h, 3)))
                    'triangle(h, 4) = striangle
                    'PRINT triangle(h, 1), triangle(h, 2), triangle(h, 3), triangle(h, 4)
                NEXT q
            NEXT p
        NEXT o
        
        'FUNCTION probtriangle(n)
        'F = 1
        'G = 6
        'h = 1
        'l = n - 3
        'FOR i = 1 TO n
        '    F = F * i
        'NEXT i
        'FOR i = 1 TO l
        '   h = h * i
        'NEXT i
        probtriangle = F / (l * G)
        'END FUNCTION
        
        
        
        FUNCTION probligne (n)
        F = 1
        G = 2
        h = 1
        l = n - 2
        FOR i = 1 TO n
            F = F * i
        NEXT i
        FOR i = 1 TO l
            h = h * i
        NEXT i
        probligne = F / (l * G)
        END FUNCTION
        


        Mon problème c'est dans la partie ou ont calcule la surface de triangle, le prog prend tous les cas possible. Avec n point il fait n !/3 !
        Mais moi je cherche que n !/((n-3)1*3 !)
        Par exemple :
        Pour 3 point ?un triangle
        Pour 4 point ? 4 triangle
        Pour 5 point ? 10 triangle ext

        Je cherche le moine de filtrer les cas qui m'intéresse pas.

        merci
        0
        1. Bonjour,

          FOR p = o TO n - 1

          c'est une erreur, il faut mettre FOR p = o+1 TO n - 1

          une autre remarque, DIM triangle(100, 4)
          normalement le tableau triangle est un tableau d'entiers qui representent les index des trois sommets des triangles, alors on devrait ecrire:
          DIM triangle(100, 3) as integer

          Les aires des triangles sont des réèls et devraient être stockées si besoin dans un tableau de réèls.

          Voilà la partie calcul d'aire remaniée:
          PRINT
          PRINT "--------------------Calcule de la surface  de triangle -----------------------"
          PRINT
          
          't = prob
          DIM triangle(1000, 3) AS INTEGER
          DIM o AS INTEGER, p AS INTEGER, q AS INTEGER
          Dim h AS INTEGER, z AS DOUBLE
          h = 0 'index triangle
          z = 0 'aire triangle
          
          FOR o = 1 TO n - 2
              
              FOR p = o + 1 TO n - 1
                 
          	FOR q = p + 1 TO n
          	    h = h + 1
          	   
          	    triangle(h, 1) = o
          	    triangle(h, 2) = p
          	    triangle(h, 3) = q
          
          	    PRINT "Triangle "; LTRIM$(STR$(h)); " (";
          	    PRINT "P"; LTRIM$(STR$(triangle(h, 1))); "-";
          	    PRINT "P"; LTRIM$(STR$(triangle(h, 2))); "-";
          	    PRINT "P"; LTRIM$(STR$(triangle(h, 3))); ")"
          
          	    xA = a(triangle(h, 1), 1): yA = a(triangle(h, 1), 2)
          	    xB = a(triangle(h, 2), 1): yB = a(triangle(h, 2), 2)
          	    xC = a(triangle(h, 3), 1): yC = a(triangle(h, 3), 2)
          
          	    PRINT "P"; LTRIM$(STR$(triangle(h, 1)));
          	    PRINT "("; STR$(xA); ";"; STR$(yA); ")   ";
          
          	    PRINT "P"; LTRIM$(STR$(triangle(h, 2)));
          	    PRINT "("; STR$(xB); ";"; STR$(yB); ")   ";
          	   
          	    PRINT "P"; LTRIM$(STR$(triangle(h, 3)));
          	    PRINT "("; STR$(xC); ";"; STR$(yC); ")"
          	    z = ABS(((xB - xA) * (yC - yA)) - ((xC - xA) * (yB - yA))) / 2
          	    PRINT "Aire :"; STR$(z)
          	    PRINT : PRINT "Appuyez sur une touche...": PRINT
          	    x$ = ""
          	    WHILE x$ = ""
          	       x$ = INKEY$
          	    WEND
          
          	NEXT q
              NEXT p
          NEXT o
          0
          1. salut a vous.
            merci Yoda

            Donc voilà en se basons sur ton aide je suis arrivée au à la fin de se exo .

            REM triqngle
            BEEP
            INPUT "n="; n
            m = 2
            h = 0
            s = 0
            u = 1
            h = 0
            k = prob(n)
            CLS
            IF n < 3 THEN
                PRINT
                PRINT "Avec le nombre de poit que vous aver donne vous pouvez pas avoire de triangle"
                PRINT
            ELSE
                RANDOMIZE TIMER
                DIM a(n, m)
                FOR i = 1 TO n
                    FOR j = 1 TO m
                        a(i, j) = INT(RND * 50)
                        'PRINT a(i, j),
                    NEXT j
                    'PRINT
                NEXT i
                '----------------------------------------------------------------
                DIM triangle(k, 7)
                DIM o AS INTEGER, p AS INTEGER, q AS INTEGER
                PRINT
                FOR o = 1 TO n - 2
                    FOR p = o + 1 TO n - 1
                        FOR q = p + 1 TO n
                            h = h + 1
                            triangle(h, 1) = a(o, 1)
                            triangle(h, 2) = a(o, 2)
                            triangle(h, 3) = a(p, 1)
                            triangle(h, 4) = a(p, 2)
                            triangle(h, 5) = a(q, 1)
                            triangle(h, 6) = a(q, 2)
                            xA = triangle(h, 1)
                            yA = triangle(h, 2)
                            xB = triangle(h, 3)
                            yB = triangle(h, 4)
                            xC = triangle(h, 5)
                            yC = triangle(h, 6)
                            s = ABS(((xB - xA) * (yC - yA)) - ((xC - xA) * (yB - yA))) / 2
                            triangle(h, 7) = s
                            'PRINT xA; yA; xB; yB; xC; yC, s
                        NEXT q
                    NEXT p
                NEXT o
                PRINT
                '----------------------------------------------------------------
                max = triangle(1, 7)
                s = 0
                FOR i = 2 TO k
                    s = triangle(i, 7)
                    IF s > max THEN u = i
                NEXT i
                PRINT "le triangle qui a la plus grande aire a pour coordoner:"
                PRINT "A("; triangle(u, 1); ","; triangle(u, 2); ")",
                PRINT "B("; triangle(u, 3); ","; triangle(u, 4); ")",
                PRINT "C("; triangle(u, 5); ","; triangle(u, 6); ")"
                PRINT "et une aire de "; triangle(u, 7)
            END IF
            
            FUNCTION prob (n)
            F = 1
            G = 6
            H = 1
            l = n - 3
            FOR i = 1 TO n
                F = F * i
            NEXT i
            FOR i = 1 TO l
                H = H * i
            NEXT i
            prob = F / (H * G)
            END FUNCTION
            0