Algorithm Analysis
We will assume that .
If , then the product of the numbers will be positive, we output 1.
If and , then there is a 0 among the factors, hence the product of the numbers equals 0, we output 0.
If , 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:
The product contains 7 negative factors. The numbers and 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);
Java Implementation
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(); } }
Python Implementation
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)