I am trying to solve this exercise, It seems easy this, but I can not understand the contraints -rules, It says:
- the number may be represented on one or two hands;
-
if the number is represented on two hands, the larger number is given first
The rule number 2 I can not understand for example if it says 3, I have 3, 2+1, 1+2 (this not because its repeated), if it says 6 we have 6, 5+1, 4+2, 3+3, 2+4 + 1+5 but the correct output is 3, can someone guide me in this problem?? for 7 is 2, and 8 is 2, 9 is 1, and 10 is 1.
this is my code:
import java.util.Scanner;
class j1 {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int tot = 5;
int n = sc.nextInt();
int sum = 0;
int count = 1;
for (int i = 1; i <= tot; i++) {
for (int j = 1; j <= tot; j++) {
sum = i + j;
if (sum == n) {
System.out.println(i);
System.out.println(j);
count++;
}
}
}
System.out.println(count);
sc.close();
}
}
Its simple – if you are going to give the number using both the hands (2 hands) then you will first need to give the larger number which comprises the overall number –
eg for 7 (4+3 OR 5+2) when represented using 2 hands – give 4 first !
other option for 7 (3+4, 2+5) are invalid since it will make us to list the smaller number first which violates the rule #2