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
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
int count = 0, result = 0;
while(count <= 10)
{
if(count % 2 ==0)
{
result = result + count;
}
else
{
result = 0;
}
count = count + 2;
}
System.out.println("Result: " + result);
Can someone explain why the answer is 30?
count starts at 0 and number 2 is added to it at each iteration. The loop stops when count is equal or higher than 10. Therefore the values of count will be :
0, 2, 4, 6, 8, 10
These are all even numbers so count % 2 == 0 will be true.
result starts at 0 and at each iteration count is added to it. Therefore the final result will be the sum of all the above numbers. And
0 + 2 + 4 + 6 + 8 + 10 = 30
To start with, count is 0. 0 % 2 == 0, so the if block is executed (and not the else block). result + count evaluates to 0, so result is set to 0. Repeat with 2 (count + 2) and this is added to result, as 2 % 2 == 0.
For every value of count, count % 2 == 0 evaluates to true. This is because you are incrementing it by 2 each time, so it will always remain even. Because of this, the else block is completely redundant and should be removed. Anyhow, what you are doing is summing all even numbers up to 10 (inclusive, as you use <=). This is 2 + 4 + 6 + 8 + 10 = 30, which is printed.
Side note: assignments which look like this (where v is a variable , o is an operator and e is an expression) may be simplified:
v = v o e;
is essentially (can anyone link me that question explaining the difference involving casting?) equivalent to
v o= e;
for arithmetic operators.
I just added two lines of code into yours and the code is now self-explaining.
int count = 0, result = 0;
while(count <= 10)
{
if(count % 2 ==0)
{
result = result + count;
System.out.println("count%2 " + count%2 + " and Count is " + count + " and Result is " + result);
}
else
{
result = 0;
System.out.println("count%2 " + count%2 + " Count is " + count + " and Result is " + result);
}
count = count + 2;
}
System.out.println("Result: " + result);
and it prints to the console following output
count%2 0 and Count is 0 and Result is 0
count%2 0 and Count is 2 and Result is 2
count%2 0 and Count is 4 and Result is 6
count%2 0 and Count is 6 and Result is 12
count%2 0 and Count is 8 and Result is 20
count%2 0 and Count is 10 and Result is 30
Result: 30
I hope this helps.
It simply increments result by every even number from zero to 10. And by adding those numbers together, you get, well, 30.
Even numbers between 0 and 10 include:
0, 2, 4, 6, 8, 10
by adding them all up, you get:
0 + 2 + 4 + 8 + 10 = 30
You can even prove this by keeping track of result as you go along. See below.
int count = 0, result = 0;
do {
if (count % 2 == 0) {
System.out.println("Result went from " + result + " to " + (result += count));
continue;
}
result = 0;
System.out.println("Result is now " + 0);
} while ((count += 2) <= 10);
System.out.println("Result: " + result);
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;
}
}
The below method return 5 if you give n = 20.
My question is how is 1 incremented on each iteration?
mystery(10) + 1
= mystery(5) + 2
= mystery(2) + 3
= mystery(1) + 4
= mystery(0) + 5 = 5.
I am having some hard time with recursion.
public static int mystery(int n){
if(n <= 0){
return 0;
}else{
return mystery(n / 2 ) + 1;
}
}
mystery(20) = mystery(10) + 1
mystery(20) = (mystery(5) + 1) + 1
mystery(20) = ((mystery(2) + 1) + 1) + 1
mystery(20) = (((mystery(1) + 1) + 1) + 1) + 1
mystery(20) = ((((mystery(0) + 1) + 1) + 1) + 1) + 1
and we know that mystery(0) = 0.
mystery(20) = ((((0 + 1) + 1) + 1) + 1) + 1
mystery(20) = (((1 + 1) + 1) + 1) + 1
mystery(20) = ((2 + 1) + 1) + 1
mystery(20) = (3 + 1) + 1
mystery(20) = 4 + 1
mystery(20) = 5
Or, simply put, we get 1+1+1+1+1=5
Pretty good video on recursion here: https://www.youtube.com/watch?v=Mv9NEXX1VHc
Looking at the code should make it obvious that:
mystery(20) = mystery(10) + 1
mystery(10) = mystery(5) + 1
mystery(5) = mystery(2) + 1
mystery(2) = mystery(1) + 1
mystery(1) = mystery(0) + 1
mystery(0) = 0
Now go back and plug in all the values, e.g.
mystery(1) = mystery(0) + 1 = 0 + 1 = 1
mystery(2) = mystery(1) + 1 = 1 + 1 = 2
mystery(5) = mystery(2) + 1 = 2 + 1 = 3, etc.
Every time mystery() is called, it returns the value returned by calling itself, plus 1. So, for every call, the returned number gets incremented by 1.