Tag Archive for problem

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

									

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