Back to site
Since 2004, our University project has become the Internet's most widespread web hosting directory. Here we like to talk a lot about web development, networking and server security. It is, after all, our expertise. To make things better we've launched this science section with the free access to educational resources and important scientific material translated to different languages.

Jeneratris Konbinezon

Source: http://www.merriampark.com/comb.htm



Michael Gilleland

Yon ti rale Sous Postal Zen Archery

CombinationGenerator Java klas la sistematik jenere tout konbinezon nan eleman n, te pran r nan yon tan. Se algorithm la dekri nan Kenneth H. Rosen, Matematik discrète ak aplikasyon li, 2yèm edisyon (NY: McGraw-Hill, 1991), pp 284-286. Klas la se yon bagay ki fasil yo sèvi ak. Sipoze ke ou vle jenere tout sa ki posib konbinezon twa-lèt nan lèt yo "yon", "b", "c", "d", "e", "f", "g". Mete lèt yo nan yon etalaj. Kenbe rele getNext () metòd dèlko a konbinezon a jiskaske pa gen okenn plis konbinezon kite. GetNext la () metòd retounen yon etalaj de nonm antye relatif, ki di ou lòd a nan sa ki pou fè aranjman pou etalaj orijinal ou lèt yo. Isit la se yon brib nan kòd ki montre kouman yo sèvi ak klas la CombinationGenerator.
Chèn [] eleman = {"yon", "b", "c", "d", "e", "f", "g"}; 
int [] endis; 
CombinationGenerator x = nouvo CombinationGenerator (elements.length, 3); 
konbinezon StringBuffer; 
pandan y ap (x.hasMore ()) { 
  konbinezon = nouvo StringBuffer (); 
  endis = x.getNext (); 
  pou (int mwen = 0; mwen <indices.length; mwen++) { 
    combination.append (eleman [endis [mwen]]); 
  } 
  System.out.println (combination.toString ()); 
}
Se yon lòt egzanp l 'a CombinationGenerator yo montre anba a an koneksyon avèk Archery Zen pwoblèm nan

Postal lan sous la gratis pou ou pou w itilize nan tou sa fason ou vle.
//-------------------------------------- 
/ / Systematic jenere konbinezon. 
/ / - ------------------------------------ 

enpòte java.math.BigInteger; 

piblik CombinationGenerator klas { 

  int prive [] yon; 
  prive int n; 
  prive r int; 
  prive BigInteger numLeft; 
  prive BigInteger total; 

  //------------ 
  / / Constructors 
  //------------ 

  piblik CombinationGenerator (int n, int r) { 
    si (r> n) { 
      voye jete nouvo IllegalArgumentException (); 
    } 
    si (n <1) { 
      voye jete nouvo IllegalArgumentException (); 
    } 
    this.n = n; 
    this.r = r; 
    yon nouvo = int [r]; 
    BigInteger nFact = getFactorial (n); 
    BigInteger rFact = getFactorial (R); 
    BigInteger nminusrFact = getFactorial (n - r); 
    total = nFact.divide (rFact.multiply (nminusrFact)); 
    reset (); 
  } 

  //------ 
  / / Reset 
  //------ 

  piblik reset anile () { 
    pou (int mwen 0 =; mwen <a.length; mwen++) { 
      yon [mwen] = mwen ; 
    } 
    numLeft = nouvo BigInteger (total.toString ()); 
  } 

  //--------------------------------- --------------- 
  / / Retounen kantite konbinezon pa ankò pwodwi 
  //------------------------ ------------------------ 

  piblik BigInteger getNumLeft () { 
    numLeft retounen; 
  } 

  //-------------- --------------- 
  / / Èske gen plis konbinezon? 
  //-------------------------- --- 

  piblik Boolean hasMore () { 
    retounen numLeft.compareTo (BigInteger.ZERO) == 1; 
  } 

  //------------------------- ----------- 
  / / Retounen kantite total konbinezon 
  //------------------------------ ------ 

  piblik BigInteger getTotal () { 
    retounen total; 
  } 

  //------------------ 
  / / Kalkile faktoryèl 
  //-------- ---------- 

  prive estatik BigInteger getFactorial (int n) { 
    BigInteger reyalite = BigInteger.ONE; 
    pou (int mwen = n; mwen> 1; mwen -) { 
      reyalite = fact.multiply (nouvo BigInteger (Integer.toString (mwen))); 
    } 
    reyalite retounen; 
  } 

  //--------------------------------- ----------------------- 
  / / générer pwochen konbinezon (algorithm soti nan Rosen p. 286) 
  //------------ -------------------------------------------- 

  piblik int [] getNext () { 

    si (numLeft.equals (total)) { 
      numLeft = numLeft.subtract (BigInteger.ONE); 
      retounen yon; 
    } 

    int mwen = r - 1; 
    pandan y ap (yon [mwen] == n - r+ mwen) { 
      mwen -; 
    } 
    yon [mwen] = yon [mwen]+ 1; 
    pou (int j = mwen+ 1; j <r; j++) { 
      yon [j] = yon [mwen]+ j - mwen; 
    } 

    numLeft = numLeft. fè soustraksyon (BigInteger.ONE); 
    retounen yon; 

  } 
}

Nan liv li Wonders nan Resansman (Oxford: Oxford University Press, 2001), pp 275-276, Clifford Pickover poze yon "Zen Archery" pwoblèm. Nan fòm pi senp lan li yo, gen yon sib ki gen 24 nimewo ki sou li. Archer an dwe tire flèch 5 nan nimewo telefòn yo sib ak frape ajoute jiska 200. Chif yo 24 sou sib la yo se
97,101,139,41,37,31,29,89,23,19,8,13, 

131,19,73,97,19,139,79,67,61,17,113,127
Pickover poze yon pwoblèm ki sanble nan Archery pa Resansman yo. Sa a se reyèlman yon pwoblèm konbinatwar - yo bay chif yo 24 te pran 5 nan yon tan, ki konbinezon inik ajoute jiska 200? Gen kèk rapid ak sal Java postal sou sit wèb la, ki asosye avèk liv Pickover a, ki rezoud pwoblèm nan archery Zen pou 24 nimewo yo bay yo. Sepandan, li se pa egzakteman yon modèl pwogram bon, epi li menm pansé ké kèk presyans nan repons lan nan postal lan, sa vle di lefèt ke tout konbinezon ajoute jiska 200 mete nimewo a 8. Sèvi ak klas CombinationGenerator nou yo, nou kapab ekri yon neater, solisyon pi jeneral de pwoblèm nan archery Zen (ZenArchery.java) jan sa a:
java.util enpòte.*; 
enpòte java.math.*; 

piblik ZenArchery abstrè klas { 

  int prive estatik getSum (vektè v) { 
    int sòm total = 0; 
    antye n; 
    pou (int mwen = 0; mwen <v.size () ; mwen++) { 
      n = (antye) v.elementAt (mwen); 
      sòm+ = n.intValue (); 
    } 
    sòm retounen; 
  } 

  piblik vektè estatik kalkile (int [] etalaj, int atATime, int desiredTotal) { 
    int [] endis; 
    CombinationGenerator gen = nouvo CombinationGenerator (array.length, atATime); 
    rezilta vektè = nouvo vektè (); 
    konbinezon vektè; 
    BigInteger numCombinations = gen.getTotal (); 
    System.out.println ("NUM konbinezon nan egzamen"+ numCombinations. toString ()); 
    int sòm total; 
    antye intObj; 
    pandan y ap (gen.hasMore ()) { 
      konbinezon = nouvo vektè (); 
      endis = gen.getNext (); 
      pou (int mwen = 0; mwen <indices.length; mwen++) { 
        intObj = nouvo antye (etalaj [endis [mwen]]); 
        combination.addElement (intObj); 
      } 
      sòm total = getSum (konbinezon); 
      si (sòm == desiredTotal) { 
        Collections.sort (konbinezon); 
        ! si (rezilta yo. gen (konbinezon)) { 
          System.out.println (combination.toString ()); 
          results.addElement (konbinezon); 
        } 
      } 
    } 
    rezilta retounen; 
  } 

  anile piblik estatik prensipal (chèn [] arg) { 
    int etalaj [] = { 97,101,139,41,37,31,29,89,23,19,8,13, 
                 131,19,73,97,19,139,79,67,61,17,113,127}; 
    rezilta vektè = ZenArchery.compute (etalaj, 5, 200); 
    System.out.println ("NUM rezilta"+ results.size ()); 
  } 

}
Isit la se pwodiksyon an soti nan klas la kouri ZenArchery ak done Pickover a:
Konbinezon NUM nan egzamen 42504 
[8, 17, 37, 41, 97] 
[8, 23, 31, 41, 97] 
[8, 13, 37, 41, 101] 
[8, 19, 31, 41, 101] 
[8, 23, 31, 37, 101] 
[8, 13, 17, 61, 101] 
[8, 13, 17, 23, 139] 
[8, 23, 41, 61, 67] 
[8, 19, 19, 41, 113] 
[8, 17, 41, 61, 73] 
[8, 13, 29, 37, 113] 
[8, 19, 23, 37, 113] 
[8, 19, 29, 31, 113] 
[8, 13, 17, 31, 131] 
[8, 13, 29, 61, 89] 
[8, 13, 23, 29, 127] 
[8, 23, 29, 67, 73] 
[8, 23, 29, 61, 79] 
[8, 13, 19, 29, 131] 
[8, 17, 19, 29, 127] 
[8, 17, 29, 67, 79] 
[8, 19, 23, 61, 89] 
[8, 13, 23, 67, 89] 
[8, 17, 19, 67, 89] 
[8, 13, 17, 73, 89] 
[8, 19, 19, 23, 131] 
[8, 17, 23, 73, 79] 
NUM rezilta 27
Published (Last edited): 08-01-2012