f6e9a
f6e9a

Reputation: 61

Counting instances in Java

So, I'm gearing up for a contest and I cant seem to figure out one of the practice problems.I got as far as importing all the data. I understand how to solve the problem in my head, just not how to put it into code. P.S. I am kind of new at programming.

The question: How many times does a dog bark in a certain time interval?

EDIT: The program should be able to work for multiple dogs

Things given to me:

  1. The total number of dogs. int numofdog = q.nextInt();

  2. The time for the first bark.

    int b = 0;
    
  3. The time between each bark.

    int d = 4;
    
  4. The time slot in which to count the number of barks. 4 - 10

So for example, There is only 1 dog. The dog first barks at 0, then at 4, then at 8.. and so on. The time slot I am counting is 4- 10. So the dog barks at 4 and 8. So in total the dog barks 2 times during 4 - 10.

I don't know how to continue to tackle this.

Original Code

import java.io.*;
import java.util.*;

public class dog {

public static void main(String[] args) {

    int b = 0;
    int d = 0;
    int x = 0;
    int y = 0;
    int count = 0;
    int m =0;
Scanner q = new Scanner(System.in);

int numofdog = q.nextInt();

while (numofdog-->0) {

     b = q.nextInt();  // First Bark
     d = q.nextInt();  // Time between barks
     x = q.nextInt();  // Starting of interval
     y = q.nextInt();  // Ending of interval    





int time [] = new int[y];             // Make an array
for (int a = 0; a < time.length; a++) {
   time[a] = a + 1;

   if (time [a] % d == 0 ) {

       count ++;
        }
    }
} 


System.out. println(count);
}

}

Updated Code (Still working on it)

I'll post my problem word by word here as well so all of us can understand

Dog Breeder bob, wants to know how many times his dog(s) bark in a certain time interval

Input. The first line of the input will be a single int numofdog, the number of dogs bob has. The next numofdog lines will each consist of two integers: b and d. b is the time of the first bark and d is the time between barks. The next line of is a single integer numberofintervals. The next numberofintervals lines will consist of two integers, x and y. x is the start of the interval being measured and y is the end

Example Input

2 --- numofdogs

0 4 --- b and d for dog 1

2 5 --- b and d for dog 2

1 --- num of intervals being measured

4 8 --- x and y for the intervals.

import java.io.*;
import java.util.*;

public class dog {

public static void main(String[] args) {

    int b1 = 0;
    int b2 = 0;
    int d1 = 0;
    int d2 = 0;
    int x1= 0;
    int y1= 0;
    int x2= 0;
    int y2= 0;
    int count = 0;


Scanner q = new Scanner(System.in);

int numofdog = q.nextInt();

while (numofdog-->0) {

     b1 = q.nextInt();  // First Bark (Dog 1)
     d1 = q.nextInt();  // Time between barks (Dog 1)
     b2 = q.nextInt();  // First Bark (Dog 2)
     d2 = q.nextInt();  // Time between barks (Dog 2)

}

    int numberofintervals = q.nextInt();

     while (numberofintervals -->0) {
     x1 = q.nextInt();  // Starting of interval
     y1 = q.nextInt();  // Ending of interval   
     x2 = q.nextInt();  // Starting of interval
     y2 = q.nextInt();  // Ending of interval   

     }

int time [] = new int[10000];             // Make an array, to represent time
for (int a = 0; a < time.length; a++) {
   time[a] = a + 1;

   if (time [a] % d1 == 0 || time [a] % d2 == 0) {

       count ++;
        }

    }
System.out.println(count);
} 



}

Upvotes: 1

Views: 220

Answers (1)

Frakcool
Frakcool

Reputation: 11153

I made a new code, you were thinking about 2 dogs, if there are 3 or more dogs, then your code won't work, so arrays are the solution to this problem.

Each dog has it's own b, d variables. Test it with more than 2 dogs and see what happens. If any error occurs just post it and I'll check it.

Your for loop wasn't the optimal since you were taking 0 as the interval start instead of your x and y (start and end) intervals.

import java.io.*;
import java.util.*;

public class NumberOfBarksTest {
    public static void main(String[] args) {
        int numOfDog = 0;
        int b[] = new int[10]; //1st bark
        int d[] = new int[10]; //time between barks
        int numberOfIntervals = 0;
        int x[] = new int[10]; //start of interval
        int y[] = new int[10]; //end of interval
        int barks[] = new int[10];
        int totalBarks = 0;
        Scanner q = new Scanner(System.in);

        for(int i = 0; i < 10; i++) {
            barks[i] = 0;
        }

        numOfDog = q.nextInt();

        for(int i = 0; i < numOfDog; i++) {
            b[i] = q.nextInt();
            d[i] = q.nextInt();
        }

        numberOfIntervals = q.nextInt();

        for(int i = 0; i < numberOfIntervals; i++) {
            x[i] = q.nextInt();
            y[i] = q.nextInt();
        }

        for(int dog = 0; dog < numOfDog; dog++) {
            for(int i = x[dog]; i <= y[dog]; i += d[dog]) {
                barks[dog]++;
            }
        }

        for(int i = 0; i < numOfDog; i++) {
            System.out.println("Barks for dog " + i + ": " + barks[i]);
            totalBarks += barks[i];
        }
        System.out.println("Total barks: " + totalBarks);
    }
}

Upvotes: 1

Related Questions