Problem 22

Back to Problem Solutions forum

Adam Lines     2015-05-15 05:22:27

Hey All,

I've attempted to use % to calculate #22, as this seemed the simplest method. The issue I'm coming up against is it only calculates the first few, then drops out for some reason. The answers are correct, it just won't finish them. Below is my code:

import java.util.*;

public class Twentytwo {

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int count = in.nextInt();

    int x, y, n, time, pages;

    for (int i = 0; i < count; i++) {
        x = in.nextInt();
        y = in.nextInt();
        n = in.nextInt();
        pages = 0;

        for (time = 1; pages < n; time++) {
            if (time % x == 0 && time % y == 0) {
                pages += 2;
            } else if (time % x == 0 || time % y == 0) {
                pages++;
            }
        }
        System.out.print((time - 1) + " ");
    }
    in.close();
}
}
Rodion (admin)     2015-05-17 06:32:26
User avatar

Hi!

Thanks for your message!

It seems you are doing the inner loop for time = 1 ... some_end_time but I'm afraid this approach will be too slow since typical answers can exceed hundreds of millions seconds - so if you mean you run the code "on site", it may terminate your program due to time limit (I'm not sure about precise value though).

I believe there should be some more efficient method, though it is arithmetics-related :)

Please login and solve 5 problems to be able to post at forum