Ver Mensaje Individual
  #3 (permalink)  
Antiguo 04/02/2012, 03:42
mandibula120
 
Fecha de Ingreso: enero-2012
Ubicación: En españa dije...
Mensajes: 28
Antigüedad: 12 años, 8 meses
Puntos: 1
Respuesta: Igualar un array en dos

Hola, gracias por contestarme. Ya estuve mirando sobre combinaciones y ya he conseguido resolverlo.


He aqui un ejemplo con 10.

Los array Locked son las personas que quiero que estén a la fuerza en un equipo.

Código C++:
Ver original
  1. /* Nombre del programa: */
  2. /* Descripción del programa: */
  3. /* Restricción de datos de entrada: */
  4. /* Fecha de creación: */
  5. /* Versión: */
  6.  
  7. /* Inclusion de archivos de cabecera */
  8. #include <iostream>
  9. #include <conio.h>
  10. #include <cstdlib>
  11. #include <math.h>
  12. #include <cstdio>
  13. #include <cstring>
  14.  
  15. using namespace std;
  16.  
  17. /* Cabeceras de nuestras propias funciones */
  18.  
  19. /* Programa principal */
  20. int main (){
  21.     /* Declaración de variables auxiliares*/
  22.     int i,j,k,l,n;
  23.     int s;
  24.     int x;
  25.     float STeam1;
  26.     float STeam2;
  27.     int TamLock1;
  28.     int TamLock2;
  29.  
  30.     /* Declaracion de variables contadoras */
  31.  
  32.     /* Declaración de variables principales */
  33.     float Slots[10]={1500,1653,1507,1506,1518,1556,1707,1680,1558,1597};  //Array with players' points.
  34.     int Locked1[5]={0,-1,-1,-1,-1}; //Players locked on team 1.
  35.     int Locked2[5]={6,7,-1,-1,-1}; //Players locked on team 2.
  36.     float Porcentajes[10]; //Array with the percentages of the players.
  37.     float Equipo1[5]; //Team 1 points.
  38.     float Equipo2[5]; //Team 2 points.
  39.     float Suma1; //Here it will be added 5 players to calculate the best error.
  40.     float SumaTotal; //All points.
  41.     float Error; //Currently error.
  42.     float ErrorFinal=100; //Final Error (The best error).
  43.     int p1[5]; //Pointer of players selected to be in team 1.
  44.     int p2[5]; //... team 2.
  45.     bool Existen; //Just to help getting "p2[]" values.
  46.     bool Verdad;
  47.  
  48.     /* Inicialización de variables */
  49.     Suma1=0;
  50.     SumaTotal=0;
  51.     STeam1=0;
  52.     STeam2=0;
  53.     TamLock1=0;
  54.     TamLock2=0;
  55.     s=1;
  56.     x=0;
  57.     k=s;
  58.  
  59.     /* Input */
  60.  
  61.     /* Procesamiento de datos */
  62.  
  63.  
  64.     /*Size of Locked Arrays*/
  65.     for(int h=0; h<5; h++){
  66.         if(Locked1[h]!=-1){
  67.             TamLock1++;
  68.         }
  69.     }
  70.     for(int h=0; h<5; h++){
  71.         if(Locked2[h]!=-1){
  72.             TamLock2++;
  73.         }
  74.     }
  75.     /*En of Size of Arrays*/
  76.  
  77.     /*Total points*/
  78.     for (i=0; i<10; i++){
  79.         SumaTotal=SumaTotal+Slots[i];
  80.     }
  81.  
  82.     /*Percentages per person*/
  83.     for (i=0; i<10; i++){
  84.         Porcentajes[i]=(Slots[i]/SumaTotal)*100;
  85.     }
  86.  
  87.  
  88.     /* BALANCE ALGORITHM*/
  89.     int m=0;
  90.     for(i=0; i<10; i++){
  91.         for(j=i+1; j<10; j++){
  92.             for(k=j+1; k<10; k++){
  93.                 for(l=k+1; l<10; l++){
  94.                     for(n=l+1; n<10; n++){
  95.                         Verdad=true;
  96.                         for(int h=0; h<TamLock2; h++){
  97.                             if(i==Locked2[h]){
  98.                                 Verdad=false;
  99.                             }
  100.                         }
  101.                         if(Verdad==true){
  102.                             Verdad=true;
  103.                             for(int h=0; h<TamLock2; h++){
  104.                                 if(j==Locked2[h]){
  105.                                     Verdad=false;
  106.                                 }
  107.                             }
  108.                             if(Verdad==true){
  109.                                 Verdad=true;
  110.                                 for(int h=0; h<TamLock2; h++){
  111.                                     if(k==Locked2[h]){
  112.                                         Verdad=false;
  113.                                     }
  114.                                 }
  115.                                 if(Verdad==true){
  116.                                     Verdad=true;
  117.                                     for(int h=0; h<TamLock2; h++){
  118.                                         if(l==Locked2[h]){
  119.                                             Verdad=false;
  120.                                         }
  121.                                     }
  122.                                     if(Verdad==true){
  123.                                         Verdad=true;
  124.                                         for(int h=0; h<TamLock2; h++){
  125.                                             if(n==Locked2[h]){
  126.                                                 Verdad=false;
  127.                                             }
  128.                                         }
  129.                                         if(Verdad==true){
  130.                                             Verdad=false;
  131.                                             for(int h=0; h<TamLock1; h++){
  132.                                                 if(i==Locked1[h] || j==Locked1[h] || k==Locked1[h] || l==Locked1[h] || n==Locked1[h]){
  133.                                                     Verdad=true;
  134.                                                 }
  135.                                             }
  136.                                             if(Verdad==true){
  137.                                                 Suma1=Porcentajes[i]+Porcentajes[j]+Porcentajes[k]+Porcentajes[l]+Porcentajes[n];
  138.                                                 Error=Suma1-50;
  139.                                                 if(Error<0){
  140.                                                     Error=Error*-1;
  141.                                                 }
  142.                                                 if(Error<ErrorFinal){
  143.                                                     ErrorFinal=Error;
  144.                                                     p1[0]=i;
  145.                                                     p1[1]=j;
  146.                                                     p1[2]=k;
  147.                                                     p1[3]=l;
  148.                                                     p1[4]=n;
  149.                                                 }
  150.                                                 m++;
  151.                                             }
  152.                                         }
  153.                                     }
  154.                                 }
  155.                             }
  156.                         }
  157.                     }
  158.                 }
  159.             }
  160.         }
  161.     }
  162.  
  163.     for(int h=0; h<10; h++){
  164.         Existen=false;
  165.         for(int c=0; c<5; c++){
  166.             if(h==p1[c]){
  167.                 Existen=true;
  168.             }
  169.         }
  170.         if(Existen==false){
  171.             p2[x]=h;
  172.             x++;
  173.         }
  174.     }
  175.  
  176.     for(int h=0; h<5; h++){
  177.         Equipo1[h]=Slots[p1[h]];
  178.     }
  179.  
  180.     for(int h=0; h<5; h++){
  181.         Equipo2[h]=Slots[p2[h]];
  182.     }
  183.  
  184.  
  185.  
  186.     /* Output */
  187.     for (i=0; i<10; i++){
  188.         cout<<Slots[i]<<" is "<<Porcentajes[i]<<"% of "<<SumaTotal<<endl;
  189.     }
  190.     cout<<"Combinations: "<<m<<endl;
  191.     cout<<"Best Error: "<<ErrorFinal<<endl<<endl;
  192.     cout<<"Total points: "<<SumaTotal<<endl<<endl;
  193.  
  194.     cout<<"Team 1"<<endl;
  195.     for(int h=0; h<5; h++){
  196.         cout<<Equipo1[h]<<endl;
  197.         STeam1=STeam1+Equipo1[h];
  198.     }
  199.     cout<<"Team 1 Points: "<<STeam1<<endl;
  200.     cout<<"Average team 1: "<<STeam1/5<<endl;
  201.  
  202.     cout<<"\nTeam 2"<<endl;
  203.     for(int h=0; h<5; h++){
  204.         cout<<Equipo2[h]<<endl;
  205.         STeam2=STeam2+Equipo2[h];
  206.     }
  207.     cout<<"Team 2 Points: "<<STeam2<<endl;
  208.     cout<<"Average team 2: "<<STeam2/5<<endl;
  209.  
  210.  
  211.     getch ();
  212.  
  213.     /* Fin de la aplicacion (entorno MS-DOS) */
  214.     return 0;
  215. }
  216.  
  217. /* Implementacion de mis funciones o procedimientos */
  218.  
  219. /***********************************************
  220.     Función X
  221.     Descripción:
  222. ************************************************/
  223. // Solución de la función o procedimiento