-
Notifications
You must be signed in to change notification settings - Fork 126
/
OrderingEvenNumbers.java
executable file
·83 lines (59 loc) · 2.22 KB
/
OrderingEvenNumbers.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
* Ordenando Números Pares e Ímpares
*
* Desafio:
* - Crie um programa onde você receberá valores inteiros
* não negativos como entrada.
*
* - Ordene estes valores de acordo com o seguinte critério:
*
* - Primeiro os Pares
* - Depois os Ímpares
*
* - Você deve exibir os pares em ordem crescente e na sequência
* os ímpares em ordem decrescente.
*
* Entrada:
* - A primeira linha de entrada contém um único inteiro positivo
* N (1 < N < 10000) Este é o número de linhas de entrada que vem
* logo a seguir. As próximas N linhas terão, cada uma delas, um
* valor inteiro não negativo.
*
* Saída:
* - Exiba todos os valores lidos na entrada segundo a ordem apresentada
* acima. Cada número deve ser impresso em uma linha, conforme exemplo de
* saída abaixo.
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class OrderingEvenNumbers {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int N = Integer.parseInt(st.nextToken());
ArrayList<Integer> entrada = new ArrayList<Integer>();
ArrayList<Integer> pares = new ArrayList<Integer>();
ArrayList<Integer> impares = new ArrayList<Integer>();
for (int i = 0; i < N; i++) {
st = new StringTokenizer(br.readLine());
entrada.add(Integer.parseInt(st.nextToken()));
if (entrada.get(i) % 2 == 0) {
pares.add(entrada.get(i));
} else {
impares.add(entrada.get(i));
}
}
Collections.sort(pares);
Collections.sort(impares);
Collections.reverse(impares);
for (int par : pares) {
System.out.println(par);
}
for (int impar : impares) {
System.out.println(impar);
}
}
}