Divide chocolate bar with if/elif/else
Question:
I’ve been having problem with this simple question. was able to solve it with for loop, but not only if/elif/else. Any suggestion on how to tackle this?
Question:
Chocolate bar has the form of a rectangle divided into n×m portions.
Chocolate bar can be split into two rectangular parts by breaking it
along a selected straight line on its pattern. Determine whether it is
possible to split it so that one of the parts will have exactly k
squares. The program reads three integers: n, m, and k. It should
print YES or NO.
my if solution, but when the input is (4,2,6), the answer is incorrect.
n = int(input())
m = int(input())
k = int(input())
if k/max(n,m) % 1 == 0 and k/max(n,m) <= min(n,m):
print('YES')
else:
print('NO')
Answers:
So the question is basically asking:
For a given n, m and k, does there exist two numbers, a and b, such that a * b = k, where either a = n and b < m or a = m and b < n.
You can solve that with this condition:
n = int(input())
m = int(input())
k = int(input())
if (k % n == 0 and k / n < m) or (k % m == 0 and k / m < n):
print('YES')
else:
print('NO')
The thing you are forgetting is that it can be cut from top-to-bottom or left-to-right.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int k = scanner.nextInt();
if(((k % n == 0) && (k/n < m)) || ((k % m == 0) && (k/m < n))) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
I’ve been having problem with this simple question. was able to solve it with for loop, but not only if/elif/else. Any suggestion on how to tackle this?
Question:
Chocolate bar has the form of a rectangle divided into n×m portions.
Chocolate bar can be split into two rectangular parts by breaking it
along a selected straight line on its pattern. Determine whether it is
possible to split it so that one of the parts will have exactly k
squares. The program reads three integers: n, m, and k. It should
print YES or NO.
my if solution, but when the input is (4,2,6), the answer is incorrect.
n = int(input())
m = int(input())
k = int(input())
if k/max(n,m) % 1 == 0 and k/max(n,m) <= min(n,m):
print('YES')
else:
print('NO')
So the question is basically asking:
For a given n, m and k, does there exist two numbers, a and b, such that a * b = k, where either a = n and b < m or a = m and b < n.
You can solve that with this condition:
n = int(input())
m = int(input())
k = int(input())
if (k % n == 0 and k / n < m) or (k % m == 0 and k / m < n):
print('YES')
else:
print('NO')
The thing you are forgetting is that it can be cut from top-to-bottom or left-to-right.
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int k = scanner.nextInt();
if(((k % n == 0) && (k/n < m)) || ((k % m == 0) && (k/m < n))) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}