(URI) Soma de Impares Consecutivos I - Solução
import java.io.*;
import java.util.*;
import java.lang.*;
class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int x = leitor(br);
int y = leitor(br);
int menor = x;
int maior = y;
if (x > y) {
menor = y;
maior = x;
}
int soma = 0;
menor++;
while (menor < maior) {
if (Math.abs(menor)%2 == 1) {
soma += menor;
}
menor++;
}
System.out.println(soma);
}
static int leitor(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;
}
}
import java.util.*;
import java.lang.*;
class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int x = leitor(br);
int y = leitor(br);
int menor = x;
int maior = y;
if (x > y) {
menor = y;
maior = x;
}
int soma = 0;
menor++;
while (menor < maior) {
if (Math.abs(menor)%2 == 1) {
soma += menor;
}
menor++;
}
System.out.println(soma);
}
static int leitor(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;
}
}
Comments
Post a Comment