Reputation: 31
This is an exercise from CodeGym.
Exercise is this:
In the main method, place plus and minus signs correctly so that the variable result is equal to 20. Signs must be placed only in the line where the variable result is declared.
Do not change the order of the variables in this line. Each variable must be preceded by either a plus or minus sign.
Requirements:
1. Values of variables: Do not change a, b, c, or d.
2. Each of the variables (a, b, c, and d) in the line where the variable result is declared must be preceded by either a plus or minus sign.
3. The program should display the number 20 on the screen.
4. The plus and minus signs must be placed correctly.
I tried to use Math.abs() to return positive number 20, but it throws an error.
I've Tried to add some variable, still throws an error.
Also, Tried to use parentheses, still same problem.
package com.codegym.task.task01.task0137;
/*
Only 20 will do
*/
public class Solution {
public static int a = 1;
public static int b = 3;
public static int c = 9;
public static int d = 27;
public static void main(String[] args)
{
int result = + a - b + c - d;
System.out.println(result);
}
}
Thanks for all! I didn't try int result = - a + b - c + d; which several of you suggested :) It was correct))
Upvotes: 2
Views: 158
Reputation: 3572
This is more of a math problem, rather than a Java/programming skill related problem.
[This question could be made a programming problem ( more interesting :D ) by making the four variables and the final output dynamic by passing those as input parameters]
The logical steps I took to solve the problem as below
given numbers are 1,3,9,27 and output should be 20, this means 27 cannot be negative in any case. so assigning + sign to 27.
take next number 9, it can only take '-' sign, otherwise the total sum will be more than 20 no matter what the signs of remaining numbers
now the result of above two steps will give 27-9 = 18. To achieve overall 20, now give + sign to 3 and - sign to 1
public class Solution {
public static int a = 1;
public static int b = 3;
public static int c = 9;
public static int d = 27;
public static void main(String[] args) {
int result = - a + b - c + d;
System.out.println(result);
}
}
Upvotes: 1
Reputation: 688
A simple math question and obviously the correct result for this assignment is as already stated:
int result = - a + b - c + d
But I really wanted to see, as a thought exercise, if I could give a solution for every combination of the preceding plus or minus sign. This does mean I had to be a little loose with the interpretation of the requirements.
My interpretation of the requirements:
Each of the variables (a, b, c, and d) in the line where the variable result is declared must be preceded by either a plus or minus sign. -> Preceded, yes, but not necessarily the first preceding operator
The program should display the number 20 on the screen. -> OK,prints the result and some more
The plus and minus signs must be placed correctly. -> Don't use the increment (++) or decrement (--) operator
This solution uses unary, arithmetic and bitwise operators to assign the value 20 to the result variable without modifying the original variables.
The printout shows the result and the current value of variables a, b, c and d, to proof that the original value is unchanged.
public class Solution {
public static int a = 1;
public static int b = 3;
public static int c = 9;
public static int d = 27;
public static void main(String[] args) {
System.out.println("--------------------------------------");
int result = + ~a + ~b + ~c ^+ ~d;
printVariables("++++", result, a, b, c, d);
result = + ~a *+ b ^+ c - d;
printVariables("+++-", result, a, b, c, d);
result = + ~a *+ b ^- ~c + ~d;
printVariables("++-+", result, a, b, c, d);
result = + ~a + b - c - ~d;
printVariables("++--", result, a, b, c, d);
result = + a - ~b ^+ ~c + d;
printVariables("+-++", result, a, b, c, d);
result = + ~a - ~b + ~c - ~d;
printVariables("+-+-", result, a, b, c, d);
result = + ~a - ~b - c + d;
printVariables("+--+", result, a, b, c, d);
result = + ~a - b ^- ~c - d;
printVariables("+---", result, a, b, c, d);
result = - ~a + b ^+ ~c + d;
printVariables("-+++", result, a, b, c, d);
result = - a + b + ~c - ~d;
printVariables("-++-", result, a, b, c, d);
result = - a + b - c + d;
printVariables("-+-+", result, a, b, c, d);
result = - a + ~b ^- ~c - d;
printVariables("-+--", result, a, b, c, d);
result = - a - ~b + ~c + d;
printVariables("--++", result, a, b, c, d);
result = - ~a - ~b ^+ ~c - ~d;
printVariables("--+-", result, a, b, c, d);
result = - a ^- ~b ^- ~c ^- d;
printVariables("----", result, a, b, c, d);
System.out.println("--------------------------------------");
}
private static void printVariables(String signs, int result, int a, int b, int c, int d) {
System.out.println("("+ signs +") Result="+ result +"; a="+ a +"; b="+ b +"; c="+ c +"; d="+ d +";");
}
}
This prints out:
--------------------------------------
(++++) Result=20; a=1; b=3; c=9; d=27;
(+++-) Result=20; a=1; b=3; c=9; d=27;
(++-+) Result=20; a=1; b=3; c=9; d=27;
(++--) Result=20; a=1; b=3; c=9; d=27;
(+-++) Result=20; a=1; b=3; c=9; d=27;
(+-+-) Result=20; a=1; b=3; c=9; d=27;
(+--+) Result=20; a=1; b=3; c=9; d=27;
(+---) Result=20; a=1; b=3; c=9; d=27;
(-+++) Result=20; a=1; b=3; c=9; d=27;
(-++-) Result=20; a=1; b=3; c=9; d=27;
(-+-+) Result=20; a=1; b=3; c=9; d=27;
(-+--) Result=20; a=1; b=3; c=9; d=27;
(--++) Result=20; a=1; b=3; c=9; d=27;
(--+-) Result=20; a=1; b=3; c=9; d=27;
(----) Result=20; a=1; b=3; c=9; d=27;
--------------------------------------
This was an interesting thought experiment. If anyone has a different and/or simpler solution, I look forward to it.
Upvotes: 0
Reputation: 3593
If you are required not to change the order of the variables, then something like this:
package com.codegym.task.task01.task0137;
public class Solution
{
public static int a = 1;
public static int b = 3;
public static int c = 9;
public static int d = 27;
public static void main(String[] args)
{
int result = -a + b - c + d;
System.out.println(result);
}
}
Upvotes: 0
Reputation: 201429
This is math, and the result should be 27 - 9 + 3 - 1
, and you can use the unary negative to make -1
. That is,
int result = -a + b - c + d;
Upvotes: 2
Reputation: 422
Try doing this.
package com.codegym.task.task01.task0137;
public class Solution
{
public static int a = 1;
public static int b = 3;
public static int c = 9;
public static int d = 27;
public static void main(String[] args)
{
int result = d - c + b - a;
System.out.println(result);
}
}
Upvotes: 0