Tag Archive for challenge

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

TJU Problem 1551 Solution

Yea… I got bored tonight, can’t sleep… solved another problem.

 

Digital Roots is a little bit tricky… if you know Machine Language (Assembly) its really simple since you’re thinking registers and ascii… if you don’t know Assembly, I dropped a hint for you… try to figure it out before looking at the solution ;)

 

C++ Solution: Simple solution using a few basic C concepts… utilizing the C-style string format, input is a long string that gets checked at each character. the sum is measured through ascii values of the characters… and so forth, I’m sure you got it by now.

#include <iostream>
#include <string>

using namespace std;

int main(){
    string s;
    cin >> s;
    while (s != "0"){
        int sum = 0;
        for(int i = 0; i < s.length() ; i++)
            sum += (s[i] - 48);
        if(sum / 10 == 0)
            cout << sum << "n";
        else{
            int root = sum;
            while(root / 10 !=0){
                sum = 0;
                while (root != 0){
                    sum += root % 10;
                    root /= 10;
                }
                root = sum;
            }
            cout << sum << "n";
        }
        cin >> s;
    }

    return 0;
}
									

**For some odd reason the code box is eliminating small things like back slashes (\). So the little typos aren’t really my fault… this question has been submitted and accepted by the TJU website. **

 

 

TJU Problem 3013 Solution

Greetings,

First problem I solve on TJU… figured I’d start posting these up here…

Alfredo’s Pizza Restaurant isn’t such a complex question, very straight forward math… There aren’t any secrets to the code really, fairly simple question to solve.

 

C++ Solution: This solution is linear, brute force solution.

#include <cstdio>
#include <iostream>
#include <cstring>
#include <cmath>

using namespace std;

int main()
{
   int r, w, l, count=1;
   double pizzaRadius=0.0;

   while(true)
   {
      scanf("%d",&r);
      if(r==0) break;
      scanf("%d%d",&w,&l);
      pizzaRadius = sqrt( (w*1.0)*(w*1.0) + (l*1.0)*(l*1.0) ) / 2.0;
      if( pizzaRadius <= r*1.0 )
         printf("Pizza %d fits on the table.n",count);
      else
         printf("Pizza %d does not fit on the table.n",count);
      count++;
   }

   return 0;
}
									

**For some odd reason the code box is eliminating small things like back slashes (\). So the little typos aren’t really my fault… this question has been submitted and accepted by the TJU website. **

 

Java Solution: It has a small bug… not in the Algorithm, but somewhere small… I wanna show an algorithm, not give code for free solutions :P

import java.util.Scanner;
import java.lang.Math;

public class TJU3013 {

  public static void main (String[] args){
    Scanner input = new Scanner(System.in);
    int count = 1;
    int r, l, w;
    double PizzaRadius = 0.0;
    while(true){
      r = input.nextInt();
      if(r == 0)
        break;
      l = input.nextInt();
      w = input.nextInt();
      PizzaRadius = Math.sqrt((w*1.0)*(w*1.0) + (l*1.0)*(l*1.0)) / 2.0;
      if(PizzaRadius <= r*1.0)
        System.out.printf("Pizza %d fits on table.n", count);
      else
        System.out.printf("Pizza %d does not fit on the table.n", count);
      count++;
    }
      
  }
}