Created
August 10, 2014 00:48
-
-
Save caiquecastro/96077b34b2fd278920c3 to your computer and use it in GitHub Desktop.
Complemento 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 Ex1 { | |
public static void main(String[] args) { | |
int valor, i; | |
Scanner in = new Scanner(System.in); | |
TreeSet<Integer> conjuntoA = new TreeSet<Integer>(); | |
boolean first = true; | |
int ceiling; | |
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); | |
ceiling = conjuntoA.last() + 10; | |
Iterator<Integer> iterator = conjuntoA.iterator(); | |
System.out.println("Conjunto A:"); | |
System.out.print("{ "); | |
while (iterator.hasNext()) { | |
if(first) { | |
System.out.print(iterator.next()); | |
} else { | |
System.out.print(", " + iterator.next()); | |
} | |
first = false; | |
} | |
System.out.println(" }\n"); | |
System.out.println("Complemento de A"); | |
System.out.print("{ "); | |
for(i = 1; i < ceiling; i++) { | |
if(!conjuntoA.contains(i)) { | |
System.out.print(i + ", "); | |
} | |
} | |
System.out.println("... }"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment