Skip to content

Instantly share code, notes, and snippets.

@caiquecastro
Created August 10, 2014 00:49
Show Gist options
  • Save caiquecastro/830838699c0ba96f7a5d to your computer and use it in GitHub Desktop.
Save caiquecastro/830838699c0ba96f7a5d to your computer and use it in GitHub Desktop.
Complemento entre Conjuntos
import java.util.Iterator;
import java.util.TreeSet;
import java.util.Scanner;
public class Ex2 {
public static void main(String[] args) {
int valor, i;
Scanner in = new Scanner(System.in);
TreeSet<Integer> conjuntoA = new TreeSet<Integer>(),
conjuntoB = new TreeSet<Integer>();
boolean first = true;
System.out.println("Digite os elementos do conjunto A:");
do {
System.out.print("Digite um valor: ");
valor = in.nextInt();
if(valor > 0)
conjuntoA.add(valor);
} while(valor > 0);
System.out.println("Digite os elementos do conjunto B:");
do {
System.out.print("Digite um valor: ");
valor = in.nextInt();
if(valor > 0)
conjuntoB.add(valor);
} while(valor > 0);
Iterator<Integer> iteratorA = conjuntoA.iterator(),
iteratorB = conjuntoB.iterator();
System.out.println("Conjunto A:");
System.out.print("{ ");
while (iteratorA.hasNext()) {
if(first) {
System.out.print(iteratorA.next());
} else {
System.out.print(", " + iteratorA.next());
}
first = false;
}
System.out.println(" }\n");
first = true;
System.out.println("Conjunto B:");
System.out.print("{ ");
while (iteratorB.hasNext()) {
if(first) {
System.out.print(iteratorB.next());
} else {
System.out.print(", " + iteratorB.next());
}
first = false;
}
System.out.println(" }\n");
first = true;
System.out.println("Complementar de B em A");
System.out.print("{ ");
iteratorA = conjuntoA.iterator();
while(iteratorA.hasNext()) {
int atual = iteratorA.next();
if(!conjuntoB.contains(atual)) {
if(first) {
System.out.print(atual);
} else {
System.out.print(", " + atual);
}
first = false;
}
}
System.out.println(" }\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment