BOTH code output will be same for any testcase , still CODE 1 submitted successfully and CODE 2 not , why??
CODE 1:
int t = in.nextInt();
while((t--)>0){
int n = in.nextInt();
for(int j=0;j<n;j++){
if(j==0 || j==n-1)
System.out.print("1");
else
System.out.print("0");
}
System.out.print("\n");
CODE 2:
int t = in.nextInt();
while((t--)>0){
int n = in.nextInt();
System.out.println((int)Math.pow(10,n-1)+1);
}
Sample test case :
2
4
3
Output:
1001
101
problem link : https://www.codechef.com/problems/ZOOZ
PLEASE OPT ME OUT , if you got the logic where i'm wrong.
Related
I am writing a simple program as some practice for Java. It takes in integers and puts them into a two-dimensional array of R rows and C columns and then simply prints out each element of the array (just for troubleshooting). When I run the code it prints every integer like it should but the program does not stop running. I have to force stop it. Why is it not stopping on its own? I tried some basic debugging and tried to see if I accidentally had an infinite loop in the code but I couldn't find it.
In case it is important I am using IntelliJ Idea Ultimate 2019.3.
Thanks
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt(); //Takes number of test cases
for(int i = 1; i <= T; i++){ //This is the counter for each test case
int R = in.nextInt(); //R is # of rows
int C = in.nextInt(); //C is # of columns
int K = in.nextInt(); //K is the thickness allowed
int numArray[][] = new int[R+1][C+1];
for(int j = 0; j < R; j++){
for(int k = 0; k < C; k++){
numArray[j][k] = in.nextInt();
System.out.println(numArray[j][k]);
}
}
}
in.close();
}//End of main
}//End of main class
Edit: Just for future reference I will include the input and the output I was getting.
Input:
3
1 4 0
3 1 3 3
2 3 0
4 4 5
7 6 6
4 5 0
2 2 4 4 20
8 3 3 3 12
6 6 3 3 3
1 6 8 6 4
Output:
3
1
3
3
4
4
5
7
6
6
2
2
4
4
20
8
3
3
3
12
6
6
3
3
3
1
6
8
6
(and here the cursor is stuck blinking not doing anything and the program doesn't quit on its own)
I solved the issue by manually typing the input instead of copying and pasting it. Not sure what caused that issue but it worked!
This little piece of code requires the user to input 3 integers (R, C and K) T-times:
int T = in.nextInt();
for(int i = 1; i <= T; i++){
int R = in.nextInt();
int C = in.nextInt();
int K = in.nextInt();
// ...
}
Additionally the user has to input an integer R*C-times until i <= T:
for(int j = 0; j < R; j++){
for(int k = 0; k < C; k++){
numArray[j][k] = in.nextInt(); // << requires user-interaction each loop here >>
System.out.println(numArray[j][k]);
}
}
If you use T=1, R=1, C=1, K=1 the user has to input only 1 additional integer, as there is only 1 test-repitition and R*C = 1. With that your program terminates just as expected.
With bigger values of T, R, C, K the required user-interactions grow exponentially, which probably makes you think that your program should have ended but it still waits for additional user-input.
Ok I think I figured out the issue. I was copying and pasting the test inputs from a web page. I tried double checking to make sure the input was valid and then inputting them by hand and it fixed it. Not 100% why but that fixes the problem. Thanks for the help!
your for(int i = 1; i <= T; i++) probably newer ends because you are increasing T.
You need to made some condition in loop to exit. For example
if (xx == yy) break;
Currently, I am doing an assignment on displaying all odd numbers up to a user inputted specific odd number. The requirement is to include the input number, however, I don't understand why does the code always goes to the next Odd number compared to what user inputted. Please help.
import java.util.Scanner;
public class OddNumbers {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Pls enter an odd number you want to finish to: ");
int capp_number = input.nextInt();
int startingNumber = 1;
for(int i = 0; i < capp_number; i+= 2){
startingNumber += 2;
System.out.println(startingNumber);
}
This is because i starts at 0 (It is even), and you are checking to see if it is less than capp_number (which should be odd), and then adding two to i. startingNumber starts at 1, so it is odd. So if the user enters 5 it will go like:
Iteration 1:
i (0) < 5 ? Yes:
print startingNumber+2
output: 3
i == 2
Iteration 2:
i (2) < 5 ? Yes:
print startingNumber +2
output: 5
i == 4
Iteration 3:
i (4) < 5 ? Yes:
print startingNumber +2
output: 7
i == 6
Iteration 4
i (6) < 5 No:
end loop
To fix this, start i at one:
for(int i = 1; i < capp_number; i+= 2)
Sorry, but why did you write this ?
while(startingNumber < capp_number);
i agree with a previous answer and think that there is no reason for this line. And also you should correct your loop as it was also mentioned in previous answer:for(int i = 0; i < capp_number; i+= 2)
Try this. You needed to initialise i to 1 within the for-loop. 1 is an odd number so instead of starting with an even number (0), you look for every odd number.
For example, if you start at 1: 1+2=3, 3+2=5, 5+2=7.... and so on.
For example, if you start at 0: 0+2=2, 2+2=4, 4+2=6.... and so on.
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("Pls enter an odd number you want to finish to: ");
int capp_number = input.nextInt();
int startingNumber = 1;
for(int i = 1; i < capp_number; i += 2){
startingNumber += 2;
System.out.println(startingNumber);
}
hey i am solving a problem on codingbat called sumDigits where u make a method which sums the digits of a number
for example
123 should return 1 + 2 +3 // 6
here is the code
public int sumDigits(int num ) // num = 123
{
int sumOfDigits = 0;
for(int i = num ; i >= 0 ; i /= 10)
{
int digit = i % 10; // stores the rightmost digit then adds it to sumOfDigits
sumOfDigits += digit;
}
return sumOfDigits;
}
Main Function:-
System.out.println("Problem 4");
Assignment5 solutionA5 = new Assignment5();
System.out.println(solutionA5.sumDigits(123));
the problem is , when i run the program
and the num parameter is = 123 as i wrote in the comment
the program waits for the user to enter another num !
i dont understand why , i didnt even include any scanner in.nextInt or anything like that ?
sorry if its a lame question
the program waits for the user to enter another num
No it doesn't. It enters an infinite loop, because i can never become negative. Your loop should be:
for (int i = num; i > 0; i /= 10)
What 'enter another num' can possibly mean in a program that doesn't accept user input at all is another mystery.
It goes into infinite loop one the value of i=0 . You shall change
for(int i = num ; i >= 0 ; i /= 10)
to
for(int i = num ; i > 0 ; i /= 10)
i didnt even include any scanner in.nextInt
Its not waiting for the user input either. All it does is this:
System.out.println("Problem 4"); // prints 'Problem 4'
Assignment5 solutionA5 = new Assignment5();
System.out.println(solutionA5.sumDigits(123)); // goes into the infinite loop
Question:
The Utopian tree goes through 2 cycles of growth every year. The first growth cycle occurs during the spring, when it doubles in height. The second growth cycle occurs during the summer, when its height increases by 1 meter.
Now, a new Utopian tree sapling is planted at the onset of the spring. Its height is 1 meter. Can you find the height of the tree after N growth cycles?
Input Format
The first line contains an integer, T, the number of test cases.
T lines follow. Each line contains an integer, N, that denotes the number of cycles for that test case.
Constraints
1 <= T <= 10
0 <= N <= 60
Output Format
For each test case, print the height of the Utopian tree after N cycles.
//FINALLY, HOPE so .. WHAT QUESTION IS SAYING..
INITIALLY VALUE IS 1 .. IF SPRING OCCURS.. IT'S VALUE WILL BE DOUBLED.. THAT MEANS .. IT WILL BE MULTIPLIED BY 2.. BUT IF SUMMER OCCUR IT'S VALUE WILL BE ADDED BY 1...
If i give input:
2 //here 2 is the number of question..
0
1
So, Output must be:
1
2
Another example,
sample of output:
2
3
4
So, Sample of input will be:
6
7
HOPE SO.. YOU UNDERSTAND WHAT QUESTION IS ASKING, HERE NOW WE HAVE TO MAKE A PROGRAM INTO JAVA....
Okay as further i made a program for this..
package com.logical03;
import java.util.Scanner;
public class MainProgram{
public static void main(String[] args){
int num=1;
int[] array=new int[100];
Scanner in=new Scanner(System.in);
System.out.println("Enter the number of Questions: ");
int n_Elements=in.nextInt();
System.out.println("Enter the values now: ");
for(int i=1; i<=n_Elements; i++){
array[i]=in.nextInt();
}
for(int i=1; i<=n_Elements; i++){
if(array[i]==0){
System.out.println("\n1");
}
else{
for(int j=1; j<=array[i]; j++){
if(j%2!=0){
num=num*2;
}
else{
num=num+1;
}
}
System.out.println(num);
}
}
}
}
As i run into here .. it adds the second number of question into my output.. Suppose..
If i give input as:
2
3
4
So, output must suppose to be:
6
7
Which is correct!!
But My program gives the output as:
6
27 //which is incorrect..becoz it adds the sum of above number :(
Mistake - int num = 1; should be declared in inside parent loop to refresh it's value.
public static void main(String[] args) {
int[] array = new int[100];
Scanner in = new Scanner(System.in);
System.out.println("Enter the number of Questions: ");
int n_Elements = in.nextInt();
System.out.println("Enter the values now: ");
for (int i = 1 ; i <= n_Elements ; i++) {
array[i] = in.nextInt();
}
for (int i = 1 ; i <= n_Elements ; i++) {
int num = 1;
if (array[i] == 0) {
System.out.println("\n1");
} else {
for (int j = 1 ; j <= array[i] ; j++) {
if (j % 2 != 0) {
num = num * 2;
} else {
num = num + 1;
}
}
System.out.println(num);
}
}
}
Output
Enter the number of Questions:
2
Enter the values now:
3
4
6
7
My approach is to take on account that first cycle (2 * height) occurs on odds indexes, and second cicle (1 + height) occurs on even indexes, from 1 to n (inclusive), starting index 0 is always 1.
return IntStream.rangeClosed(1, n)
.reduce(1, (acc, idx) -> idx % 2 != 0 ? acc * 2 : acc + 1);
This is my first contribution, only learning to code and solve algorithms, I had to find a workable solution with simple to follow code credit to http://www.javainterview.net/HackerRank/utopian-tree
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
//receive input
Scanner in = new Scanner(System.in);
//no of test cases
int T=in.nextInt();
//no of cycles
int[] N = new int[T];
for(int i=0;i<T;i++){
N[i]=in.nextInt();
}
int height=1;
for(int i=0;i<N.length;i++){
height=1;
for(int j=1;j<=N[i];j++){
if((j%2) ==1)
height=height*2;
else
height++;
}
System.out.println(height);
}
}
}//this the end of the class
been through making a few logical code just cant figure out how to get the output of 01 triangle in the desired form the triangle does print out but not according to the require output.
import java.util.Scanner;
import java.io.*;
public class triangle10{
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.println("Enter a number ");
int num =input.nextInt();
while(num > 0){
for(int j=1;j<=num;j++){
System.out.print(num);
}
System.out.print("\n");
num--;
}
}
}
==================================================================
if the user gives input as 6
the out put should be :
111111
00000
1111
000
11
0
Instead of
for(int j=1;j<=num;j++){
System.out.print(num);
}
I think you need to print either a 1 or a 0 (according to your example). So you should compute whether num is even or odd, and decide whether to print 0 or 1. For example
for(int j=1;j<=num;j++){
System.out.print(1 - num%2);
}
Because num%2 = 0 when num is even, and 1 if it is odd - and it seems you want the opposite. So 1 - num%2 should give you what you need.
Before:
Enter a number
6
666666
55555
4444
333
22
1
After:
Enter a number
6
111111
00000
1111
000
11
0
Change you code like this :
boolean flag = false;
while(num > 0){
flag = !flag;
for(int j=1;j<=num;j++){
if(flag==true)
System.out.print("1");
else
System.out.print("0");
}
System.out.print("\n");
num--;
}
You just need to make a small change
From
for(int j=1;j<=num;j++){
System.out.print(num);
}
to
for (int j = 1; j <= num; j++) {
System.out.print((num-1)%2);
}
And then you will get the expected result:
Take entering 6 for example:
Enter a number
6
111111
00000
1111
000
11
0
You are printing out num. Instead you should print out 1 or 0 depending on num.
Try this :
while(num > 0){
for(int j=1;j<=num;j++){
int digit = (num + 1) % 2 ;
System.out.print(digit);
}
System.out.print("\n");
num--;
}