Tag Archive for tju

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--;
        }
    }
}

									

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;
        }
    }
}

									

TJU Problem 2501 Solution

Score, a relatively simple problem in the sense that it’s mainly tracing through a string… I solved this one in Java just because it seemed a little more challenging, even though once you use .toCharArray() the difference between java and C++ goes out the window… I tried keeping it simple, after all it was a problem in a programming competition and I didn’t want to spend too much time on it… so here’s the way I solved it.

It’s not necessarily the pretties code you’ll ever see, but it works.

Enjoy!

/*
*  TJU Problem 2501: Score
*  http://acm.tju.edu.cn/toj/showp2501.html
*/

import java.util.Scanner;

//gotta rename it main.java to submit...
public class p2501 {

    public static void main (String args[]){
        Scanner scn = new Scanner(System.in);
        final int cases = Integer.parseInt(scn.nextLine());
        for(int i = 0; i <= cases; i++){
            int finalAnswer = 0;
            int counter = 0;
            final char[] string = scn.nextLine().toCharArray();
            if(string[0] == 'O'){
                counter++;
                finalAnswer= finalAnswer + counter;
            }
            for(int j = 1; j< string.length; j++){
                if(string[j] == 'O' && string[j-1] == string[j])
                    counter++;
                else if(string[j] == 'O')
                    counter = 1;
                else
                    counter = 0;
                finalAnswer= finalAnswer + counter;
            }
            System.out.println(finalAnswer);
        }
    }
}