URI Online Judge | 1099
Sum of Consecutive Odd Numbers II
Adapted by Neilor Tonin, URI Brazil
Timelimit: 1
Read an integer N that is the number of test cases. Each test case is a line containing two integer numbers Xand Y. Print the sum of all odd values between them, not including X and Y.
Input
The first line of input is an integer N that is the number of test cases that follow. Each test case is a line containing two integer X and Y.
Output
Print the sum of all odd numbers between X and Y.
Input Sample | Output Sample |
7 4 5 13 10 6 4 3 3 3 5 3 4 3 8 | 0 11 5 0 0 0 12 |
URI Online Judge Solution 1099 || Sum of Consecutive Odd Numbers II in JAVA
import
java.io.IOException;
import
java.util.Scanner;
public
class
Main {
public
static
void
main(String[] args)
throws
IOException {
int
N;
int
interval_start,interval_end;
Scanner input =
new
Scanner(System.in);
N =input.nextInt();
for
(
int
i =
1
; i <= N; i++) {
int
total_odd =
0
;
interval_start = input.nextInt();
interval_end = input.nextInt();
if
(interval_start > interval_end) {
for
(
int
j = interval_start-
1
; j > interval_end; j--) {
if
(j %
2
!=
0
) {
total_odd+=j;
}
}
}
else
if
(interval_start < interval_end){
for
(
int
j = interval_start +
1
; j < interval_end; j++) {
if
(j %
2
!=
0
) {
total_odd+=j;
}
}
}
else
{
total_odd =
0
;
}
System.out.print(total_odd+
"\n"
);
}
}
}
Download code from dropbox
Main Question link
here has vary bad add
ReplyDelete