(SPOJ) 1334 - Calculando - 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);
}
void processa() throws NumberFormatException, IOException {
String line = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int contador = 0;
while ((line = br.readLine()) != null) {
StringTokenizer tokenizer = new StringTokenizer(line);
int numOperandos = Integer.valueOf(tokenizer.nextToken());
if (numOperandos == 0) {
return;
}
contador++;
System.out.println("Teste " + contador);
line = br.readLine();
line = "+" + line;
String num = "";
int resultado = 0;
boolean entrouWhile = false;
for (int i = 0; i < line.length(); i++) {
if (line.charAt(i) == '+') {
int j = i+1;
while (j < line.length() && line.charAt(j) != '+' && line.charAt(j) != '-') {
num += line.charAt(j);
j++;
entrouWhile = true;
}
if (entrouWhile) {
i = j-1;
entrouWhile = false;
}
resultado += Integer.valueOf(num);
num = "";
}
else if (line.charAt(i) == '-') {
int j = i+1;
while (j < line.length() && line.charAt(j) != '+' && line.charAt(j) != '-') {
num += line.charAt(j);
j++;
entrouWhile = true;
}
if (entrouWhile) {
i = j-1;
entrouWhile = false;
}
resultado -= Integer.valueOf(num);
num = "";
}
}
System.out.println(resultado);
System.out.println();
}
return;
}
}
import java.util.*;
class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
Main processando = new Main();
processando.processa();
System.exit(0);
}
void processa() throws NumberFormatException, IOException {
String line = "";
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int contador = 0;
while ((line = br.readLine()) != null) {
StringTokenizer tokenizer = new StringTokenizer(line);
int numOperandos = Integer.valueOf(tokenizer.nextToken());
if (numOperandos == 0) {
return;
}
contador++;
System.out.println("Teste " + contador);
line = br.readLine();
line = "+" + line;
String num = "";
int resultado = 0;
boolean entrouWhile = false;
for (int i = 0; i < line.length(); i++) {
if (line.charAt(i) == '+') {
int j = i+1;
while (j < line.length() && line.charAt(j) != '+' && line.charAt(j) != '-') {
num += line.charAt(j);
j++;
entrouWhile = true;
}
if (entrouWhile) {
i = j-1;
entrouWhile = false;
}
resultado += Integer.valueOf(num);
num = "";
}
else if (line.charAt(i) == '-') {
int j = i+1;
while (j < line.length() && line.charAt(j) != '+' && line.charAt(j) != '-') {
num += line.charAt(j);
j++;
entrouWhile = true;
}
if (entrouWhile) {
i = j-1;
entrouWhile = false;
}
resultado -= Integer.valueOf(num);
num = "";
}
}
System.out.println(resultado);
System.out.println();
}
return;
}
}
Comments
Post a Comment