Last active
May 17, 2018 23:33
-
-
Save brunoarmanelli/12b82a2006cb78a4d2f3c9ad50da754e to your computer and use it in GitHub Desktop.
Solução do CLSLDR - Class Leader (SPOJ)
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.Scanner; | |
| /** | |
| * Maratona de Programação - Laboratório II | |
| * Problema Class Leader | |
| * Bruno Armanelli @ PUC Minas | |
| */ | |
| class No { | |
| No proximo; | |
| int id; | |
| } | |
| class ListaCircular { | |
| private No no; | |
| public ListaCircular() { | |
| this.no = null; | |
| } | |
| public int executaMaratona(int m, int o) { | |
| while(no.id != m) { | |
| no = no.proximo; | |
| } | |
| while(no != no.proximo) { | |
| for(int i = 0; i < o; i++) { | |
| no = no.proximo; | |
| } | |
| remover(no.id); | |
| } | |
| return no.id; | |
| } | |
| public void remover(int x) { | |
| while(no.proximo.id != x) { | |
| no = no.proximo; | |
| } | |
| no.proximo = no.proximo.proximo; | |
| } | |
| public void add(int value) { | |
| No node = new No(); | |
| node.id = value; | |
| if (this.no == null) { | |
| node.proximo = node; | |
| this.no = node; | |
| } else { | |
| node.proximo = this.no.proximo; | |
| this.no.proximo = node; | |
| } | |
| } | |
| } | |
| class Principal { | |
| private static int resolve (int n, int m, int o) { | |
| ListaCircular alunos = new ListaCircular(); | |
| // Preenche lista com total de alunos | |
| for(int i = n; i > 0; i--) { | |
| alunos.add(i); | |
| } | |
| // Executa dentro da lista circular | |
| int resultado = alunos.executaMaratona(m, o); | |
| return resultado; | |
| } | |
| public static void main(String[] args) { | |
| // TODO Auto-generated method stub | |
| int resultado; | |
| String parametros[]; | |
| Scanner input = new Scanner (System.in); | |
| String line; | |
| int T, n, o, m, j = 0; | |
| line = input.nextLine(); | |
| T = Integer.parseInt(line); | |
| while(j < T) { | |
| line = input.nextLine(); | |
| parametros = line.split(" "); | |
| n = Integer.parseInt(parametros[0]); //numero de alunos | |
| m = Integer.parseInt(parametros[1]); //primeiro aluno que inicia com o papel | |
| o = Integer.parseInt(parametros[2]); //quantidade ate o proximo aluno | |
| resultado = resolve(n, m, o); | |
| System.out.println(resultado); | |
| j++; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment