Tag Archive for java

TJU Problem 2123 Solution – Java

Head or Tail is a simple counting problem… count 0s or 1s and output the final counts… code’s below.

/*
 * Author: Gal Appelbaum
 * www.galappelbaum.com
 */
import java.util.*;

public class p2123 {

    public static void main(String args[]){
        Scanner scn = new Scanner(System.in);
        int games = scn.nextInt();
        int currentGame;
        while(games != 0){
            int Mary = 0;
            int John = 0;
            while(games != 0 ){
                currentGame = scn.nextInt();
                if(currentGame == 0)
                    Mary++;
                else
                    John++;
                games--;
            }
            System.out.printf("Mary won %d times and John won %d timesn", Mary, John);
            games = scn.nextInt();
        }
    }
}

									

TJU Problem 2001 Solution – Java

Counting Sheep is a very straight forward problem, basic string matching. My solution’s below.

/*
 * Author: Gal Appelbaum
 * www.galappelbaum.com
 */
import java.util.Scanner;

public class p2001 {

    public static void main(String args[]){
        Scanner scn = new Scanner(System.in);
        int cases = scn.nextInt();
        int testCase = 0;
        while(cases !=0){
            int wordCount = scn.nextInt();
            int total = 0;
                String word;
                while(wordCount != 0){
                    word = scn.next();
                if (word.equals("sheep"))
                    total += 1;
                wordCount--;
            }
                System.out.printf("Case %d: This list contains %d sheep.n", ++testCase, total);
            if(cases != 1)
                System.out.println();
            cases--;
        }
    }
}

									

Project Euler Problem 14 Solution – Longest Collatz sequence

Longest Collatz sequence is a very cool problem which I first encountered (requiring different things, but method was the same) when I started learning C++. The problem states that for any set of positive integers you do the following:

n → n/2 (n is even)
n → 3n + 1 (n is odd)

and that eventually, any number will end up at 1. the problem requires us to count how many numbers are in the chain between n and 1… the solution to this classic project Euler problem is below in Java.

 

/*
 * Author: Gal Appelbaum
 * www.galappelbaum.com
 */

public class p14 {

    public static void main(String[] args){
        int longestChain = 0;
        int finalNumber = 0;
        int count = 1;
        double currentNumber = 0;
        for(int i = 2; i < 1000000; i++){
            currentNumber = i;
            while(currentNumber != 1){
                if(currentNumber % 2 == 0)
                    currentNumber = currentNumber/2;
                else
                    currentNumber = 3*currentNumber + 1;
                count++;
            }
            if(count > longestChain){
                longestChain = count;
                finalNumber = i;
            }
            count = 0;
            System.out.println(i);
        }
        System.out.println(finalNumber);
    }
}

									

TJU Problem 2955 Solution – Java

CETVRATA,  is a pretty straight forward question… some (like me) might not realize the extreme simplicity of it at first, but once you realize you’re over thinking it it’s a fairly easy programming problem.

While the question tries to make you think about which corners of the rectangle you have and maybe you’re thinking along that direction, the answer lies within the numbers and their order…  My solution to problem 2955 in java is below.

import java.util.Scanner;

public class p2955 {

    public static void main(String[] args){
        Scanner src = new Scanner(System.in);
        int a = src.nextInt(); // Left
        int b = src.nextInt(); // Right
        int c = src.nextInt(); // Left
        int d = src.nextInt(); // Right
        int e = src.nextInt(); // Left
        int f = src.nextInt(); // Right
        int answer1, answer2 = 0;
        if(a == c)
            answer1 = e;
        else if (a == e)
            answer1 = c;
        else
            answer1 = a;
        if(b == d)
            answer2 = f;
        else if(b == f)
            answer2 = d;
        else
            answer2 = b;
        System.out.printf("%d %dn", answer1, answer2);
    }
}

									

TJU Problem 2120 Solution – Java

Psuedo-Random Numbers is a fairly straight forward question but it has one main trick that even I didn’t notice initially… The last sentence of the explanation says “But be careful — the cycle might not begin with the seed!” and that’s the trick.

 

Essentially the example shows you how to solve this problem if the seed is the first number, but what should be assumed from the problem is that the random number sequence has “cycles” which will eventually restart. so the trick is to figure out if the new number you’ve generated has been generated before, and if it did, you’re done. All it is is a simple array that is as big as M (read the problem…), and every time a number is calculated and added to the array, backtrack to make sure it wasn’t there before. if it was, the arrays length from the point it was originally generated until now is your answer… here’s a simple solution in Java.

import java.util.Scanner;

public class p2120 {

    public static void main(String[] args){
        boolean run = true;
        boolean done = false;
        int Z, I, M, L = 0;
        int caseNumber = 1; 
        int answer = 0;
        Scanner src = new Scanner(System.in);
        while(run){
            Z = src.nextInt();
            I = src.nextInt();
            M = src.nextInt();
            L = src.nextInt();
            if(Z == 0 && I == 0 && M == 0 && L == 0)
                break;
            int[] results = new int[M];
            for(int i = 0; i <= M; i++){
                results[i] = (Z*L+I) % M;
                L = results[i];
                if(i > 0){
                    for(int j = i-1; j >= 0; j--){
                        if(results[j] == results[i]){
                            answer = i - j;
                            done = true;
                            break;
                        }  
                    }
                }
                if(done)
                    break;
            }
            System.out.printf("Case %d: %dn", caseNumber, answer);
            caseNumber++;
            answer = 0;
            done = false;
        }
    }
}