Skip to content

Instantly share code, notes, and snippets.

@GeorgePaiva
Last active June 24, 2020 13:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GeorgePaiva/852cb3696ab2bebc63a193e3442d3c73 to your computer and use it in GitHub Desktop.
Save GeorgePaiva/852cb3696ab2bebc63a193e3442d3c73 to your computer and use it in GitHub Desktop.
package desafiometa;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Questao03 {
public static void main(String[] args) {
ArrayList<Integer> input = new ArrayList<Integer>();
int diaCompra = 0;
int diaVenda = 0;
int compra = 0;
int venda = 0;
System.out.println("Digite o tamanho do array: ");
int size = Integer.parseInt(new Scanner(System.in).nextLine());
System.out.println("Digite os valores do array: ");
for (int i = 0; i < size; i++) {
input.add(Integer.parseInt(new Scanner(System.in).nextLine()));
}
for (int i = 0; i < input.size(); i++) {
if (input.get(i).equals(Collections.min(input))) {
diaCompra = i + 1;
compra = input.get(i);
i = diaCompra;
} else if (i + 1 < size) {
if (input.get(i).compareTo(input.get(i + 1)) > 0) {
if (compra > 0) {
venda = input.get(i);
diaVenda = i + 1;
}
}
}
}
if (venda - compra > 0) {
System.out.print(String.format((venda - compra) + " (Comprou no dia " + diaCompra + " (preço igual a "
+ compra + ")" + " e vendeu no dia " + diaVenda + " (preço igual " + venda + ")," + " lucro foi de "
+ venda + " - " + compra + " = " + (venda - compra)));
} else {
System.out.print(String.format((("0 (Nesse caso nenhuma transação deve ser feita, lucro máximo igual a 0)" ))));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment