public class Solution {
public static void main(String[] args) {
int x = 1;
int y = 0;
y = y + x;
x = x * 2;
y = y + x;
x = x * 2;
y = y + x;
//x = x * 2;
y = y + x;
x = x * 2;
y = y + x;
System.out.println(y);
}
srivani
Level 2
what is the solution for this?
Resolved
Comments (13)
- Popular
- New
- Old
You must be signed in to leave a comment
Beautiful Active Dedicated
19 April 2019, 00:46
0
Serg
18 April 2019, 13:00
make comments to each line with the results of the calculation and very quickly find a solution.
y = y + 3 * x; // 3
x = x * 2; // 2
x = x * 16; // 18 or 16
etc.
0
ROBERT KOSS
1 March 2019, 18:37
instead of doing it complicated ill tell it as simple as it is. keep 3 that way it can be a 9
+1
Manny Salamanca
23 October 2018, 04:44
package com.codegym.task.task02.task0211;
/*
The required number
*/
public class Solution {
public static void main(String[] args) {
int x = 1;
int y = 0;
y = y + x;
x = x * 2;
y = y + x;
x = x * 2;
// y = y + x;
x = x * 2;
y = y + x;
// x = x * 2;
y = y + x;
System.out.println(y);
}
}
0
hidden #10359466
16 October 2018, 02:57solution
Here's a suggested path that can help to figure out the solution.
1. Jot down the different lines on which the assignment statements are written in the question.
2. Do an x and y table
3. Comment out one or more of the assignment statements to be used (x or y will not change its value)
4. Write the value of x or y calculated for each assignment statement
5. Keep going until you get y = 19.
This is a helpful way of obtaining a solution (there are multiple solutions to this question).
Sample:
Line x y
9 & 10 1 0
12 1 0
14 2 0
15 2 2
17 2 2 (commented out line 17)
18 2 4
20 4 4
21 4 8
23 8 8
24 8 16
Y is 16.
+3
hidden #10362262
16 October 2018, 03:02
Looks like there is a solution that works with 1 comment and another solution that works with 2 comments. But the lesson won't pass with the 1 comment for some reason.
+2
hidden #10362262
10 October 2018, 19:35
I am trying this, but it seems as if there are 512 different combinations if the commented out sections are represented as 0's and 1's in an array. Is there an easier way to get to the solution other than trying out all 512 combinations?
This is NOT a solution below, just a suggestion to track the variables as you comment out.
public class Solution {
public static void main(String[] args) {
int x = 1;
int y = 0;
y = y + x;
System.out.print(x);
System.out.print(" ");
System.out.println(y);
x = x * 2;
System.out.print(x);
System.out.print(" ");
System.out.println(y);
y = y + x;
System.out.print(x);
System.out.print(" ");
System.out.println(y);
x = x * 2;
System.out.print(x);
System.out.print(" ");
System.out.println(y);
y = y + x;
System.out.print(x);
System.out.print(" ");
System.out.println(y);
x = x * 2;
System.out.print(x);
System.out.print(" ");
System.out.println(y);
y = y + x;
System.out.print(x);
System.out.print(" ");
System.out.println(y);
x = x * 2;
System.out.print(x);
System.out.print(" ");
System.out.println(y);
y = y + x;
System.out.print(x);
System.out.print(" ");
System.out.println(y);
System.out.println(y);
}
}
+3
Shahzeb
28 September 2018, 02:57
comment out the line 18 and 21
That's It
+2
Khurram
4 September 2018, 04:53
dry run the program and calculate the values of x and y after each statement, when you reach the end you should be able to figure out what the values should be such that y = 19 and then comment out the unnecessary lines.
0
bilalkhan8
1 September 2018, 19:46
/* The comment was deleted */
+2
Roman
3 September 2018, 11:04
Posting the solution in the comments is prohibited.
0
Usman Jutt
28 August 2018, 11:51
you have to comment out 2 lines
+1