Ajuda com time limit execeeded

0 views
Skip to first unread message

Rafael Henrique Mendes de Oliveira

unread,
Sep 28, 2014, 8:56:54 AM9/28/14
to sifase2010.2
Olá, bom dia. Estou resolvendo algumas questões no URI
https://www.urionlinejudge.com.br/

Tenho um código mas não sei como otimizar ele para executar em menos tempo. Alguém pode me ajudar?



import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
     
public class Main {
    public static void main(String[] args) throws IOException {
        InputStreamReader ir = new InputStreamReader(System.in);
        BufferedReader in = new BufferedReader(ir);
        while(true){
            String[] str = in.readLine().split(" ");
            if(str[0].equals("EOF") || str[0].equals("") || str[0].equals(" "))
                break;
            int[][] matriz = null;
            if(Integer.parseInt(str[0]) >= 3 && Integer.parseInt(str[0]) < 70){
                matriz = new int[Integer.parseInt(str[0])][Integer.parseInt(str[0])];
                int jTemp = Integer.parseInt(str[0])-1;
                for (int i = 0; i < Integer.parseInt(str[0]); i++) {
                        for (int j = 0; j < matriz.length; j++) {
                            matriz[i][j] = 3;
                            if(i == j)
                                matriz[i][j] = 1;
                        }
                        matriz[i][jTemp] = 2;
                        jTemp--;
                }
                imprimir(matriz);
            }
        }
    }
    public static void imprimir(int[][] matriz){
        for (int i = 0; i < matriz.length; i++) {
            for (int j = 0; j < matriz.length; j++) {
                if(matriz.length-1 == j)
                    System.out.println(matriz[i][j]);
                else
                    System.out.print(matriz[i][j]);
            }
        }
    }
}

Reply all
Reply to author
Forward
0 new messages