We will assume that a \leq b.
If a > 0, then the product of the numbers will be positive, we output 1.
If a < 0 and b > 0, then there is a 0 among the factors, hence the product of the numbers equals 0, we output 0.
If b < 0, then all the factors are negative. The sign of the product is determined by the number of negative factors. If the number of factors is even ( and have different parity), then the answer is 1. Otherwise, the answer is -1.
Example
In the third example, the product looks like: (-7) * (-6) * (-5) * (-4) * (-3) * (-2) * (-1)
The product contains 7 negative factors. The numbers a = -7 and b = -1 have the same parity. Therefore, the product is negative.
Algorithm Implementation
Read the input data.
scanf("%d %d", &a, &b);If all factors are positive, then the answer is 1.
if (a > 0) res = 1;If there is a 0 among the factors, then the answer is 0.
else if (a <= 0 && b >= 0) res = 0;If all factors are negative, then the answer is determined by the number of factors. If the number of factors is even ( and have different parity), then the answer is 1. Otherwise, the answer is -1.
else
{
if ((b - a) % 2 == 0) res = -1;
else res = 1;
}Output the answer.
printf("%d\n", res);import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner con = new Scanner(System.in);
int a = con.nextInt();
int b = con.nextInt();
int res;
if (a > 0) res = 1;
else if (a <= 0 && b >= 0) res = 0;
else
{
if ((b - a) % 2 == 0) res = -1;
else res = 1;
}
System.out.println(res);
con.close();
}
}a, b = map(int, input().split())
if a > 0: res = 1
elif a <= 0 and b >= 0: res = 0
else:
if (b - a) % 2 == 0: res = -1
else: res = 1
print(res)