Spoiler alert!

Looking at this page will give you a possible answer, and spoil the fun. So while you can, go back!


Some of the problems I’ve solved on DMOJ.

Infinity

Link to the problem

Infinity.java:

// import statements
import java.util.Scanner;

public class Infinity{
    public static void main(String[] args) {
        // initialise scanner
        Scanner in = new Scanner(System.in);
        
        // read the 2 input numbers as hex (base16)
        int a = in.nextInt(16);
        int b = in.nextInt(16);

        // check if the sum of te 2 input numbers is bigger than our
        // infinity (everything bigger than the 32bit int limit)
        if (a+b > 0x3F3F3F3F){
            // if so, print "Yes"
            System.out.println("Yes");
        } else {
            // if it's not, print "No"
            System.out.println("No");
        }
        // close scanner in, end of program
        in.close();
    }
}

Phantom1

Link to the problem

Phantom1.java:

// import statements
import java.util.Scanner;

public class Phantom1 {

    // method to check for a prime.
    // takes number, returns true if it's a prime.
    private static boolean isPrime(int n){
	    //check if n is a multiple of 2
	    if (n%2==0) return false;
	    //if not, then just check the odds
	    for(int i=3;i*i<=n;i+=2) {
	        if(n%i==0)
	            return false;
	    }
	    return true;
    }

    public static void main(String[] args) {

        // initialise scanner
        Scanner in = new Scanner(System.in);

        // read inputs
        // first input is the number of intervals that will follow
        // n is [1,10]
        int n = in.nextInt();
        int[] p = new int[n];
        // loop trough each set of input
        for (int i = 0; i < n; i++) {

            // read inputs.
            // [a,b)
            // a = left boundry
            // b = right boundry
            int a = in.nextInt();
            int b = in.nextInt();
            // check for primes within the interval.
            int c = 0; // count of primes.
            for (int j = a; j < b; j++) {

                // check if j is a prime
                if (isPrime(j)){
                    c++;
                }

            }
            // save to the output array.
            p[i] = c;

        }

        // print output
        for (int i = 0; i < n; i++) {
            System.out.println(p[i]);
        }

        // close scanner on the end.
        in.close();

    }

}

Binary

Link to the problem

Binary.java:

// import statements
import java.util.Scanner;

public class Binary{
    public static void main(String[] args){
        // initialise Scanner
        Scanner in = new Scanner(System.in);

        //read input
        int n = in.nextInt();

        // create empty string for the output.
        String o = "";

        // read next n integers
        for(int i = 0; i < n; i++){
            // read input (decimal);
            int d = in.nextInt();
            
            // convert the integer to a binary string
            String b = Integer.toBinaryString(d);

            // Add padding to the string
            if (b.length() % 4 != 0){
                for (int j = 0; j < b.length()%4; j++){
                    b = "0"+b;
                }
            }

            //turn the string into an array of characters
            String[] ba = b.split("");

            // split the string into groups of 4
            String b2 = "";
            for (int j = 0; j < b.length(); j++){
                if (j%4 == 0 && j != 0){
                    // add space
                    b2 += " "+ ba[j];
                } else {
                    // just add the number
                    b2 += ba[j];
                }
            }

            // add the result to the output
            o += String.format("%s%n",b2);

        }

        // print output
        System.out.print(o);

        // close scanner on the end of the program
        in.close();
    }
}