(URI) Soma de Ímpares Consecutivos II - Solução 1
import java.io.*;
import java.util.*;
import java.math.*;
class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int entradas = leitor(br);
for (int i = 0; i < entradas; i++) {
int x = leitor(br);
int y = leitor(br);
int maior = Math.max(x, y);
int menor = Math.min(x, y);
int soma = 0;
for (int j = menor+1; j < maior; j++) {
if (j%2 == 1) {
soma += j;
}
}
bw.write(soma+"\n");
}
bw.flush();
bw.close();
}
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.math.*;
class Main {
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int entradas = leitor(br);
for (int i = 0; i < entradas; i++) {
int x = leitor(br);
int y = leitor(br);
int maior = Math.max(x, y);
int menor = Math.min(x, y);
int soma = 0;
for (int j = menor+1; j < maior; j++) {
if (j%2 == 1) {
soma += j;
}
}
bw.write(soma+"\n");
}
bw.flush();
bw.close();
}
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