0% found this document useful (0 votes)
26 views5 pages

Exercicios

The document contains 7 coding exercises in Portuguese that involve reading input from a scanner, sorting or ordering data structures, and other basic programming challenges. The exercises get more advanced and involve different data structures like vectors, tree sets, and arrays.

Uploaded by

Murilo Zanardo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views5 pages

Exercicios

The document contains 7 coding exercises in Portuguese that involve reading input from a scanner, sorting or ordering data structures, and other basic programming challenges. The exercises get more advanced and involve different data structures like vectors, tree sets, and arrays.

Uploaded by

Murilo Zanardo
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

Exercicio 1

import java.util.Scanner;
import java.util.Vector;
import java.util.Collections;

public class OrdenacaoDeAlunos {


public static void main(String[] args) {

Scanner scanner = new Scanner(System.in);

System.out.print("Digite o número de alunos: ");


int n = scanner.nextInt();
scanner.nextLine();

Vector<String> nomes = new Vector<String>();

for (int i = 0; i < n; i++) {


System.out.print("Digite o nome do aluno " + (i+1) + ": ");
String nome = scanner.nextLine();
nomes.add(nome);
}
Collections.sort(nomes);

System.out.println("\nLista de nomes ordenados:");


for (String nome : nomes) {
System.out.println(nome);
}

scanner.close();
}
}

Exercicio 2:

import java.util.Scanner;
import java.util.TreeSet;

public class OrdenacaoDeAlunos {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Digite o número de alunos: ");
int n = scanner.nextInt();
scanner.nextLine(); // Consumir a quebra de linha

TreeSet<String> nomes = new TreeSet<String>();


for (int i = 0; i < n; i++) {
System.out.print("Digite o nome do aluno " + (i+1) + ": ");
String nome = scanner.nextLine();
nomes.add(nome);
}
System.out.println("\nLista de nomes ordenados:");
for (String nome : nomes) {
System.out.println(nome);
}

scanner.close();
}
}

Exercicio 3:

import java.util.Arrays;
import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; ++i) {
a[i] = scanner.nextInt();
}
Arrays.sort(a);
int res = 0;
for (int i = 0; i < n; i += 2) {
res += a[i + 1] - a[i];
}

System.out.println(res);

scanner.close();
}
}

Exercício 4:

38598654 1069 Diamantes e Areia Accepted Java 19 0.486 28/03/2024


20:21

Fiz na sala de aula antes da Sexta Santa, confundi os exercícios.

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int N = scanner.nextInt();
scanner.nextLine();
for (int k = 0; k < N; ++k) {
String expressao = scanner.nextLine();

int resposta = 0;
int parenteses = 0;
int tam = expressao.length();
for (int i = 0; i < tam; ++i) {
if (expressao.charAt(i) == '<') {
++parenteses;
} else if (expressao.charAt(i) == '>' && parenteses > 0) {
--parenteses;
++resposta;
}
}
System.out.println(resposta);
}

scanner.close();
}
}

Exercício 5:

import java.util.*;

class Comp implements Comparator<String> {


@Override
public int compare(String s1, String s2) {
if (s1.length() != s2.length()) {
return Integer.compare(s1.length(), s2.length());
} else {
return 0;
}
}
}

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int casosTeste = scanner.nextInt();


scanner.nextLine();

for (int t = 0; t < casosTeste; t++) {


String linha = scanner.nextLine();
String[] pals = linha.split(" ");
List<String> palavras = Arrays.asList(pals);
palavras.sort(new Comp());

Iterator<String> iterator = palavras.iterator();


while (iterator.hasNext()) {
System.out.print(iterator.next());
if (iterator.hasNext()) {
System.out.print(" ");
}
}
System.out.println();
}

scanner.close();
}
}

Exercício 6:
import java.util.Scanner;

public class Pomekon {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

String[] pomekons = new String[1001];


int N, p;
String S;

N = scanner.nextInt();
scanner.nextLine();

p = 0;
for (int i = 0; i < N; ++i) {
S = scanner.nextLine();

if (busca(pomekons, S, N) == -1) {
pomekons[p++] = S;
}
}

System.out.printf("Falta(m) %d pomekon(s).\n", 151 - p);


}

public static int busca(String[] pomekons, String pomekon, int n) {


for (int i = 0; i < n; ++i) {
if (pomekons[i] != null && pomekons[i].equals(pomekon)) {
return i;
}
}

return -1;
}
}

Exercício 7:
import java.io.IOException;
import java.util.Scanner;

public class URI 2727 {

public static void main(String[] args) throws IOException {


Scanner leitor = new Scanner(System.in);
while (leitor.hasNext()) {
int N = leitor.nextInt();
for (int i = 0; i < N; i++) {
String[] codigo = readLine(leitor).split(" ");
char letra = (char) (96 + (((codigo.length - 1) * 3) +
codigo[0].length()));
System.out.println(letra);
}
}
}

public static String readLine(Scanner leitor) {


String line = leitor.nextLine();
while (line.isEmpty())
line = leitor.nextLine();
return line;
}
}

You might also like