Created
August 10, 2014 00:50
-
-
Save caiquecastro/01cfd5c9b8c724de8b6a to your computer and use it in GitHub Desktop.
Intersecção de Conjuntos
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Iterator; | |
import java.util.TreeSet; | |
import java.util.Scanner; | |
public class Ex4 { | |
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("Intersecção de A com B"); | |
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