terça-feira, 28 de fevereiro de 2017

União entre dois arrays de inteiros

Dados os conjuntos A e B, define-se como
união dos conjuntos A e B ao conjunto
representado por todos os elementos
pertencentes a A mais B.
Na prática se houver elementos iguais,
só um de cada repetido entra no conjunto C.
Talvez você já saiba fazer isto,
más se não souber aproveite meu claro
exemplo que disponibilizo para seu aprendizado.

Veja abaixo uma imagem do programa em execução:


Veja abaixo o código do programa:



import java.util.Scanner;

public class PROJETO {
     public static int tam = 20;

     public static void main ( String args [ ] ) {
         J jht = new J ( );
         jht.textbackground ( jht.WHITE );
         jht.Moldura ( ( short ) 1, ( short ) 24, ( short ) 2, ( short ) 78 );
         jht.title ( " " );
         int z = 0, y = 0;
         int a = 0;
         int [ ] A = new int [ tam ];
         int [ ] B = new int [ tam ];
         int [ ] C = new int [ tam ];
         int i, p, q, n = 0;
         do {
              jht.cls ( );
              jht.textcolor ( jht.LIGHTRED );
              jht.gotoxy ( ( int ) 22, ( int ) 3 );
              System.out.printf ( "UNIÃO ENTRE DOIS ARRAYS DE INTEIROS" );
              jht.textcolor ( jht.LIGHTBLUE );
              jht.gotoxy ( ( int ) 25, ( int ) 5 );
              System.out.printf ( "Quantos números terá o array A ? " );
              jht.textcolor ( jht.LIGHTRED );
              Scanner sc = new Scanner ( System.in );
              p = sc.nextInt ( );
              jht.textcolor ( jht.BLACK );
              jht.gotoxy ( ( int ) 20, ( int ) 9 );
              if ( p < 0 || p > tam ) {
                   System.out.printf ( "Número Acima de 0 e menor que %d ", tam );
                   jht.getche ( );
                   jht.cls ( );
              }
         } while ( ( p < 0 ) || ( p > tam ) );
         for ( i = 0; i < p; i++ ) {
              jht.cls ( );
              jht.textcolor ( jht.LIGHTRED );
              jht.gotoxy ( ( int ) 22, ( int ) 3 );
              System.out.printf ( "UNIÃO ENTRE DOIS ARRAYS DE INTEIROS" );
              jht.textcolor ( jht.LIGHTBLUE );
              jht.gotoxy ( ( int ) 32, ( int ) 5 );
              System.out.printf ( "Insira o " );
              jht.textcolor ( jht.LIGHTRED );
              jht.gotoxy ( ( int ) 40, ( int ) 5 );
              System.out.printf ( " %do", i + 1 );
              jht.textcolor ( jht.LIGHTBLUE );
              jht.gotoxy ( ( int ) 44, ( int ) 5 );
              System.out.printf ( "Número : " );
              jht.textcolor ( jht.LIGHTRED );
              Scanner sc = new Scanner ( System.in );
              A [ i ] = sc.nextInt ( );
         }
         do {
              jht.cls ( );
              jht.textcolor ( jht.LIGHTRED );
              jht.gotoxy ( ( int ) 22, ( int ) 3 );
              System.out.printf ( "UNIÃO ENTRE DOIS ARRAYS DE INTEIROS" );
              jht.textcolor ( jht.LIGHTBLUE );
              jht.gotoxy ( ( int ) 25, ( int ) 5 );
              System.out.printf ( "Quantos números terá o array B ? " );
              jht.textcolor ( jht.LIGHTRED );
              Scanner sc = new Scanner ( System.in );
              q = sc.nextInt ( );
              jht.textcolor ( jht.BLACK );
              jht.gotoxy ( ( int ) 20, ( int ) 9 );
              if ( q < 0 || q > tam ) {
                   System.out.printf ( "Número Acima de 0 e menor que %d ", tam );
                   jht.getche ( );
                   jht.cls ( );
              }
         } while ( ( q < 0 ) || ( q > tam ) );
         for ( i = 0; i < q; i++ ) {
              jht.cls ( );
              jht.textcolor ( jht.LIGHTRED );
              jht.gotoxy ( ( int ) 22, ( int ) 3 );
              System.out.printf ( "UNIÃO ENTRE DOIS ARRAYS DE INTEIROS" );
              jht.textcolor ( jht.LIGHTBLUE );
              jht.gotoxy ( ( int ) 32, ( int ) 5 );
              System.out.printf ( "Insira o " );
              jht.textcolor ( jht.LIGHTRED );
              jht.gotoxy ( ( int ) 40, ( int ) 5 );
              System.out.printf ( " %do", i + 1 );
              jht.textcolor ( jht.LIGHTBLUE );
              jht.gotoxy ( ( int ) 44, ( int ) 5 );
              System.out.printf ( "Número : " );
              jht.textcolor ( jht.LIGHTRED );
              Scanner sc = new Scanner ( System.in );
              B [ i ] = sc.nextInt ( );
         }
         jht.textcolor ( jht.LIGHTBLUE );
         jht.gotoxy ( ( int ) 22, ( int ) 7 );
         System.out.printf ( "Números inseridos no array A ==> " );
         jht.textcolor ( jht.LIGHTRED );
         System.out.printf ( "%d", p );
         jht.textcolor ( jht.BLACK );
         jht.gotoxy ( ( int ) 22, ( int ) 9 );
         for ( i = 0; i < p; i++ ) {
              System.out.printf ( " %d ", A [ i ] );
         }
         jht.textcolor ( jht.LIGHTBLUE );
         jht.gotoxy ( ( int ) 22, ( int ) 11 );
         System.out.printf ( "Números inseridos no array B ==> " );
         jht.textcolor ( jht.LIGHTRED );
         System.out.printf ( "%d", q );
         jht.textcolor ( jht.BLACK );
         jht.gotoxy ( ( int ) 22, ( int ) 13 );
         for ( i = 0; i < q; i++ ) {
              System.out.printf ( " %d ", B [ i ] );
         }
         jht.getche ( );
       ////////////////////////////////////////////////////////////////////
         int V = p + q;
         int j = 0, k = 0, h = 0, aux = 0, auxin = 0, vezes, temp;
         //Concatenando array A e array B em array C.
         for ( i = 0; i < p; i++ )
              C [ i ] = A [ i ];
         for ( i = p; i < V; i++ )
              C [ i ] = B [ i - p ];
         //Se houver repetidos exclui
         int x = 0;
         for ( x = 0; x < V; x++ ) {
              vezes = 1;
              j = x + 1;
              while ( j < V )
                   if ( C [ j ] != C [ x ] )
                        j++;
                   else {
                        vezes++;
                        //exclui
                        V--;
                        C [ j ] = C [ V ];
                   }
         }
       ////////////////////////////////////////////////////////////////////
         jht.textcolor ( jht.LIGHTBLUE );
         jht.gotoxy ( ( int ) 22, ( int ) 15 );
         System.out.printf ( "Abaixo a união dos dois arrays" );
         jht.textcolor ( jht.BLACK );
         jht.gotoxy ( ( int ) 22, ( int ) 17 );
         for ( i = 0; i < V; i++ ) {
              System.out.printf ( " %d ", C [ i ] );
         } 
         jht.textcolor ( jht.LIGHTBLUE );
         jht.gotoxy ( ( int ) 22, ( int ) 19 );
         jht.Sleep ( ( short ) 1800 );
         System.out.printf ( "Por: " );
         jht.textcolor ( jht.LIGHTMAGENTA );
         System.out.printf ( "Samuel Lima" );
         jht.textcolor ( jht.BLACK );
         jht.gotoxy ( ( int ) 22, ( int ) 20 );
         System.out.printf ( "sa_sp10@hotmail.com" );
         jht.Sleep ( ( short ) 1800 );
         jht.textcolor ( jht.LIGHTRED );
         jht.gotoxy ( ( int ) 34, ( int ) 22 );
         System.out.printf ( "MUITO OBRIGADO" );
         jht.getche ( );
     }
}