I have encountered strange arithmetic operations and here is the code:
int i = 9 + + 8 - - 11 + + 13 - - 14 + + 15;
System.out.println(i);
It works without compilation error and gives the output 70, I have tried to google but did not find the correct explanations. Please pardon me I am new to Java.
int i = 9 + + 8 - - 11 + + 13 - - 14 + + 15;
is equivalent to
int i = 9 + (+8) - (-11) + (+13) - (-14) + (+15);
which is equivalent to
int i = 9 + 8 + 11 + 13 + 14 + 15;
which is equal to 70
Actually it is mathematical arithmetic operation and the same applies in Java:
- - = +
+ + = +
int i = 9 + 8 + 11 + 13 + 14 + 15;
so it is 70
9+ +8 is equivalent to 9+(+8) and
8- -11 is equivalent to 8-(-11)
so 9 + + 8 - - 11 + + 13 - - 14 + + 15 is equivalent to 9+(+8)-(-11)+(+13)-(-14)+(+15)
which is equivalent to 9+8+11+13+14+15 = 70
Related
I've been given a problem about finding all the possible combinations to change a 5 euro note. I've written a program that fails to result the correct number of combinations.
My approach was inspired from the following:
500 can be divided into 200, 200 and 100.
200 can be divided into 100 and 100.
100 can be divided into 50 and 50.
After I've written my code, I've realised that 100 can also be divided into 5 20's. This is a fault which I am aware of but I do not know how to fix using my approach.
My approach was a recursive one as can be seen below, it simply checks the first digit and divides it accordingly.
Here is what I tried:
public class Q1 {
public static int counter;
public static void main(String[] args) {
divide(500);
System.out.println(counter);
}
private static void divide(int x) {
System.out.println("Dividing " + x);
if(x == 1) {
return;
}
counter++;
int length = String.valueOf(x).length();
int fd = Integer.parseInt(Integer.toString(x).substring(0, 1));
String zeros;
if(fd != 1) {
zeros = Integer.toString(x).substring(1, length);
}else {
zeros = Integer.toString(x).substring(1, length-1);
}
if(fd == 5) {
divide(Integer.parseInt(2 + "" + zeros));
divide(Integer.parseInt(2 + "" + zeros));
divide(Integer.parseInt(1 + "" + zeros));
}else if(fd == 2) {
divide(Integer.parseInt(1 + "" + zeros));
divide(Integer.parseInt(1 + "" + zeros));
}else if(fd == 1) {
divide(Integer.parseInt(5 + "" + zeros));
divide(Integer.parseInt(5 + "" + zeros));
}
}
}
For example using the above program misses
10 = 2 + 2 + 2 + 2 + 2
I am aware of working solutions already present like this one but I would like to maintain my approach if possible.
Using the program to finding out the combinations for 500 cents results 388 ways, where the correct answer is 6295435. Something tells me I'm forgetting something else other than the above example.
Here are some hints about why you get the wrong number:
A correct way to determine all possibilities
Try to split 5 instead of 500 for simplicity. Notice that there are 4 possibilities, namely 5 =
5
2 + 2 + 1
2 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1
Now try dividing 10 instead of 500.
Notice that this can be split up into 11 different ways:
10 =
10
5 + 5
5 + 2 + 2 + 1
5 + 2 + 1 + 1 + 1
5 + 1 + 1 + 1 + 1 + 1
2 + 2 + 2 + 2 + 2
2 + 2 + 2 + 2 + 1 + 1
2 + 2 + 2 + 1 + 1 + 1 + 1
2 + 2 + 1 + 1 + 1 + 1 + 1 + 1
2 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1
This solution follows the following pattern:
The number x you want to split is already an answer.
Decrease the amount of lowest numbers by 1 by splitting one of these numbers into as many next largest numbers as possible. Always ignore ones. If there is only one number (and ones) left, split x into as many next lowest numbers as possible and continue.
For example, x = 10. Then: 10 is the lowest number -> split it up into 5 + 5 -> 5 is the lowest number ->split it up into 2 + 2 + 1 -> 2 is the lowest number since ones are ignored -> split it up into 1 + 1 -> we have another 2, split it up into 1 + 1 (this is equal to the solution 5 + 1 + 1 + 1 + 1 so now we have 5 as only one number apart from ones. Next lowest number is 2)-> split x=10 into 2 + 2 + 2 + 2 + 2 -> 2 is the lowest number; split it into 1 + 1 -> we have another 2 ...
This can be done in an recursive approach.
Things that went wrong in your code
What you code does with the example of dividing 10 is the following:
10 is divided into 5 + 5
the first 5 is divided into 2 + 2 + 1
one of these twos is divided into 1 + 1
the other two is divided into 1 + 1
Now, the second 5 from the division into 5 + 5 is divided into 2 + 2 + 1
One of these twos is divided into 1 + 1
The other two is divided into 1 + 1
Giving it a total score of 7 possibilities.
Trying to map These 7 possibilities to the 11 above, you count 10 =
10 once
5 + 5 once
5 + 2 + 2 + 1 twice
5 + 2 + 1 + 1 + 1 twice
5 + 1 + 1 + 1 + 1 + 1 twice
and missing the other 6 options.
So the assumption that this question can be solved by an approach like this one:
10 = 5 + 5 -> evaluate the first 5, then evaluate the second 5
is wrong because in both cases this leads to a distribution of 10 = 5 + evaluation of 5, counting only the options where at least one 5 is contained in the final distribution of 10 (counting it multiple times whearas distributions without fives are not evaluated).
Another mistake is that the code says there is no possible distribution of 1 where there actually exists one (1 = 1).
Also, the question is unclear about
can x equal all possible whole numbers? -> 4 is not split up into 2 + 2 in your Code
is 1 + 1 + 2 a different solution from 1 + 2 + 1 and 2 + 1 + 1?
(I don't have the reputation to ask this in a comment yet).
Remaining an recursive approach can only be done with some significant changes. A possible way to do so is stated above.
Unable to determine how the output comes?
I ran this code and the answer comes as 75:
Please tell the logic for this.
public class MainClass{
public static void main(String[] args)
{
int i = 10 + + 11 - - 12 + + 13 - - 14 + + 15;
System.out.println(i);
}
}
You have to understand how to operators is work ;
int i = 10 + +11 - -12 + +13 - -14 + +15;
10 +
11 (- - =>) +
12 (+ + =>) +
13 (- - =>) +
14 (+ + =>) +
15
The end result is :
int i = 10 + 11 + 12 + 13 + 14 + 15;//===> 75
In Java, a - - b is parsed as a - ( -b ). The first - is subtraction. The second one is negation. But (from high school maths) subtracting a negative number is the same as adding a positive one ...
Likewise, a + + b is parsed as a + ( +b ) .... etcetera.
Warning: If you ever write code like that in real life, the velociraptors will get you: https://xkcd.com/292/
Here -- acts as + and ++ acts as + (Basic mathematics logic).
So basically you are making sum of all numbers.
Logically
int i = 10 + + 11 - - 12 + + 13 - - 14 + + 15; // i=75
Becomes :-
int i = 10 + 11 + 12 + 13 + 14 + 15; // i = 75
I executed this in C++ and I found the same answer that is 75.
What I concluded is,
it checks the operators - - if we multiply the signs then it becomes + plus therefore it adds all the numbers.
I tried a counter example for you that is
int i = 10 + +11 - -12 + +13 - +14 + +15;
its output is 47 which is 10 + 11 + 12 + 13 - 14 + 15. that means in this case (-,+) with 14 becomes - so it subtracts 14 from the amount calculation starting from left according to rules.
Thanks for improving knowledge.
public class MainClass {
public static void main(String[] args)
{
int i = 10 + + 11 - - 12 + + 13 - - 14 + + 15;
System.out.println(i);
}
}
what is happening here?
It is that 10 + (+ 11) and what are we able to see 10 + + 11
from this operation it will get the value as 21
Then coming to second 21 - (-12) but we are seeing it as 10 + + 11 - - 12 = 33
Then Third case 33 + + 13 but it is 10 + + 11 - - 12 + + 13 = 46
Then coming to fourth 46 - (-14) but we are seeing it as 10 + + 11 - - 12 + + 13 - - 14 = 60
Then coming to fifth 60 + (+14) but we are seeing it as 10 + + 11 - - 12 + + 13 - - 14 + + 15=75
This is all happening because - - is addition
This question already has answers here:
In Java, is the result of the addition of two chars an int or a char?
(8 answers)
Closed 5 years ago.
Can anyone explain why adding an empty single quotation (at the end of this code) creates 32? I have changed the integers 3 and 4 and every time I do this, the base number seems to be 32.
public static void main(String[] args) {
int number = 5;
System.out.println("Initial Value: " + number);
number = number*2;
number = number*2;
System.out.println("\n1. After doubling it twice: " + number);
number = number + 3;
number += 3;
System.out.println("\n2. After adding 3 twice: " + number);
number -= 12;
System.out.println("\n3. After subtracting 12: " + number);
number = number / 3;
System.out.println("\n4. After dividing by 3: " + number);
System.out.println();
number ++;
System.out.println("add 1: " + number);
number ++;
System.out.println("add 1: " + number);
number ++;
System.out.println("add 1: " + number);
number ++;
System.out.println("add 1: " + number);
System.out.println("\n5. After adding 1 four times: " + number);
number -= 1;
System.out.println("\n6. After decrementing once: " + number);
int remainder = number%= 3;;
System.out.println("\n7. Remainder when dividing by 3 is :" + remainder);
int a = 2, b = 3, c = 5;
double d1, d2, d3, d4;
d1 = a + b * c / 2;
d2 = (a + b * c) / 2;
d3 = (a + b) * c / 2;
d4 = (a + b) * (c / 2);
System.out.println("\n8. Values: " + d1 + " : " + d2 + " : " + d3
+ " : " + d4);
int p, q;
p = 10;
q = 10;
p += q++;
System.out.println("\n9. Result is: " + (p + q));
double d7 = 4.3, d8 = 34.7;
double truncatedSum = (4.3 + 34.7);
System.out.println("\n10. Sum is " + truncatedSum);
System.out.println("\n11.");
System.out.println("fred " + 3 + 4);
System.out.println(3 + 4 + " fred");
System.out.println("" + 3 + 4);
**System.out.println(' ' + 3 + 4);**
}
OUTPUT
Initial Value: 5
After doubling it twice: 20
After adding 3 twice: 26
After subtracting 12: 14
After dividing by 3: 4
add 1: 5
add 1: 6
add 1: 7
add 1: 8
After adding 1 four times: 8
After decrementing once: 7
Remainder when dividing by 3 is :1
Values: 9.0 : 8.0 : 12.0 : 10.0
Result is: 31
Sum is 39.0
11.
fred 34
7 fred
34
39
Here the thing
System.out.println(' ' + 3 + 4);
' ' is of char type which is a numeric data type in Java:
char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
and it corresponds to a numeric value of space character in UNICODE chart, e.g. 32.
Therefore, the result of this computation will be 32 + 3 + 4 or 39 as you can see in your output.
' ' + 3 + 4
as chars are numeric types in Java, ' ' stands for space character int value
Space is 32 in ASCII, so the result is:
32 + 3 + 4 = 39
how do i get the max and min number of nodes in an AVL tree when given the height of 8.
i can't seem to be able to trace it out properly from the formula f(8)=f(7)+f(6)+1
2*f(6)+f(5)+2
2*[f(5)+f(4)+1]+f(5)+2
3*f(5)+2*f4+4
3*[f(4)+f(3)+1]+2*f(4)+4
5*f(4)+3*f(3)+7
5*[f(3)+f(2)+1]+3*f(3)+7
8*f(3)+5*f(2)+12
8*[f(2)+f(1)+1]+5*f(2)+12
13*f(2)+8*f(1)+20
13*[f(1)+f(0)+1]+8*f(1)+20
21*f(1)+13*f(0)+33=54 whereas answer is 88 is the minimum
For every node in AVL tree we know that the depths of the left and the right subtree differs by at most 1 (this is given by definition).
From that, the next step is quite obvious : we take the minimum trees of depths N and N - 1 and place them as subtrees for a new root. It's clear that the AVL rules still hold and that the tree is contains as little nodes as possible (obvious from the induction base case).
From that, we've got the recursion formula : minnodes(depth) = 1 + minnodes(depth-1) + minnodes(depth - 2). That's a simple recursive equation, Wolfram Alpha can solve that for you (link).
The second case is trivial - a perfect binary tree of depth h contains as many nodes as possible for the depth given and trivially satisfies the AVL conditions.
You miscalculated a step somewhere, looks like near the end:
f(0) = 1
f(1) = 2
f(2) = f(1) + f(0) + 1 = 4
f(3) = f(2) + f(1) + 1 = 4 + 2 + 1 = 7
f(4) = f(3) + f(2) + 1 = 7 + 4 + 1 = 12
f(5) = f(4) + f(3) + 1 = 12 + 7 + 1 = 20
f(6) = f(5) + f(4) + 1 = 20 + 12 + 1 = 34
f(7) = f(6) + f(5) + 1 = 34 + 20 + 1 = 55
f(8) = f(7) + f(6) + 1 = 55 + 34 + 1 = 88
And if you don't believe it, you can always cook up a quick snippet to check:
#Test
public void testGetMax() {
assertEquals(88, getMax(8));
}
int getMax(int x) {
switch (x) {
case 0:
return 1;
case 1:
return 2;
default:
return getMax(x - 1) + getMax(x - 2) + 1;
}
}
Mathematical Expression
1 + + 1 results 2
1 + - 1 returns 0
1 + - + 1 returns 0
can anybody know the reason of this. because I only know ++ -- operation but in this case the operator is '+ +' and still not giving an error.
Its because + is also a unary operator which means positive, just like - means negative.
1 + + 1 = 1 + (+1) = 1 + 1 = 2
1 + - 1 = 1 + (-1) = 1 - 1 = 0
1 + - + 1 = 1 + -(+1) = 1 + -1 = 1 - 1 = 0;
Unary + and - operators at work here.
1 + (+1) = 2
1 + (-1) = 0
1 + (-(+1)) = 0
JLS §15.15.3