[Beginner] Java Baekjun #3052, I wonder why it's not working.

Asked 2 years ago, Updated 2 years ago, 22 views

Hello, I am Jarin, who has been studying Java for 3 days now. There are many answers when I google it, but I'm so curious why I can't solve it.

This is question 3052 from Java When there are two natural numbers A and B, A%B is the remainder of A divided by B. For example, 7, 14, 27, 38 divided by 3 and the remainder is 1, 2, 0, and 2.

After receiving 10 numbers, find the remainder divided by 42. Then write a program that outputs how many different values there are.

I solved the problem like this, but I get a runtime error.

import java.util.Scanner;

public class Main {
  public static void main(String[] args) {

    Scanner scanner = new Scanner(System.in);

    int[] arr = new int[10];
    int count = 0;

    for ( int i = 0; i < 10; i++ ) {
      arr[i] = scanner.nextInt() % 42;
    }

    for ( int i = 0; i < 10; i++ ) {
        if ( arr[i] != arr[ i+1] ) {
         count++;
        }
      }
    }

    System.out.println(count);

  }
}

Why can't you count when i and i+1 are different...?

java

2022-09-20 11:37

1 Answers

If it's 0, 1, 0, 1, 0, 1, it's two, but your code is five.


2022-09-20 11:37

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.