Tag Archive for solution

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 6 Solution – Sum Square Difference

Sum Square Difference also known as problem 6 is very straight forward math… (A) calculate the summation of the numbers squared, and (B)then the square of the summation… then subtract A from B… my solution for this problem is below in C++

#include <iostream>
#include <string>
#include <sstream>

using namespace std;


int main(){
    int sumfirsthundred = 0;
    int squarefirsthundred = 0;
    int sumfirsthundredsqr = 0;
    for(int i = 1; i < 101; i++){
        sumfirsthundred = sumfirsthundred + i;
        squarefirsthundred = squarefirsthundred + (i*i);
    }
    sumfirsthundredsqr = sumfirsthundred * sumfirsthundred;
    cout << sumfirsthundredsqr - squarefirsthundred << "n";
    std::cout << "Press ENTER to continue...";
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), 'n' );
    return 0;
}
									

 

Project Euler Problem 7 Solution – 10001st Prime

10001st Prime is another prime calculating problem… nothing hard. my solution is below and written in C++.

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int isPrime(int number){
int count=0;
for(int a = 1; a<=number ;a++)
{
if(number % a == 0)
count++;
}
if(count == 2)
return number;
return 0;
}

int main(){
int primecount = 0;
int num = 1;
int prime = 0;
bool working = true;
while(primecount != 10001){
prime = isPrime(num);
num++;
if(prime > 0){
primecount++;
}
}

cout << prime <<"n";
std::cout << "Press ENTER to continue...";
std::cin.ignore( std::numeric_limits<std::streamsize>::max(), 'n' );
return 0;
}

 
									

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