(SPOJ) 1745 - Recuperação - Solução
import java.io.*;
import java.util.*;
class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
Main processando = new Main();
processando.processa();
System.exit(0);
}
static int gambis(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
int sinal = 1;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') break;
if (n == '-') sinal = -1;
if (n == '+') sinal = 1;
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') break;
}
return resp*sinal;
}
void processa() throws NumberFormatException, IOException {
String line = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int contador = 0;
while((line = br.readLine()) != null) {
int qteValores = Integer.parseInt(line);
int soma = 0;
int valorSatisfaz = -5000;
boolean primeiraVez = true;
for (int i = 0; i < qteValores; i++) {
int valor = gambis(br);
if (primeiraVez && valor == soma) {
valorSatisfaz = valor;
primeiraVez = false;
}
soma += valor;
}
contador++;
if (valorSatisfaz != -5000) {
bw.write("Instancia " + contador + "\n" + valorSatisfaz + "\n\n");
}
else {
bw.write("Instancia " + contador + "\nnao achei\n\n");
}
}
bw.flush();
bw.close();
return;
}
}
import java.util.*;
class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
Main processando = new Main();
processando.processa();
System.exit(0);
}
static int gambis(BufferedReader br) throws NumberFormatException, IOException {
int n;
int resp = 0;
int sinal = 1;
while (true) {
n = br.read();
if (n >= '0' && n <= '9') break;
if (n == '-') sinal = -1;
if (n == '+') sinal = 1;
}
while (true) {
resp = resp*10 + n-'0';
n = br.read();
if (n < '0' || n > '9') break;
}
return resp*sinal;
}
void processa() throws NumberFormatException, IOException {
String line = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int contador = 0;
while((line = br.readLine()) != null) {
int qteValores = Integer.parseInt(line);
int soma = 0;
int valorSatisfaz = -5000;
boolean primeiraVez = true;
for (int i = 0; i < qteValores; i++) {
int valor = gambis(br);
if (primeiraVez && valor == soma) {
valorSatisfaz = valor;
primeiraVez = false;
}
soma += valor;
}
contador++;
if (valorSatisfaz != -5000) {
bw.write("Instancia " + contador + "\n" + valorSatisfaz + "\n\n");
}
else {
bw.write("Instancia " + contador + "\nnao achei\n\n");
}
}
bw.flush();
bw.close();
return;
}
}
Comments
Post a Comment