Skip to content

Commit

Permalink
Added 2003 Solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
johnafish committed Feb 18, 2016
1 parent 1922f29 commit cb51239
Show file tree
Hide file tree
Showing 11 changed files with 805 additions and 0 deletions.
39 changes: 39 additions & 0 deletions 2003/Junior/J1.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
% CCC 2003
% Problem J1: Trident
%
% Simple "*" printing program. Lots of Loops :-)
% for the height of the tines, spaces between and
% height of the handle.
%
% keyboard and screen I/O

var t, s, h : int
put "Enter tine length:"
get t
put "enter tine spacing:"
get s
put "Enter handle length:"
get h
for i : 1 .. t
put "*" ..
for j : 1 .. s
put " " ..
end for
put "*" ..
for j : 1 .. s
put " " ..
end for
put "*"
end for
for i : 1 .. 2 * s + 3
put "*" ..
end for
put ""
for i : 1 .. h
for j : 1 .. s + 1
put " " ..
end for
put "*"
end for


27 changes: 27 additions & 0 deletions 2003/Junior/J2.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
% CCC 2003
% Problem J2: Picture Perfect
%
% A rectangle of a given area has
% has a minimum perimeter if arranged as a square (or closest to it)
% so find the square root of the area (number of pictures)
% and then find the largest integer <= that square root
% That solves the problem
%
% keyboard and screen I/O

var a, l, w : int
loop
put "Enter number of pictures:"
get a
exit when a = 0
l := round (sqrt (a))
loop
exit when a mod l = 0
l := l - 1
end loop
w := a div l
put "Minimum perimeter is ", 2 * l + 2 * w, " with dimensions ", w,
" X ", l
put ""
end loop

40 changes: 40 additions & 0 deletions 2003/Junior/J3.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
% CCC 2003
% Problem J3/S1: Snakes and Ladders
%
% simple dice roll input, move the piece. watch for 3 snakes and 3 ladders
% also watch for going over 100. Declare if win or quit.
%
% keyboard and screen I/O

var place, x : int
place := 1
loop
put "Enter sum of dice:"
get x
if x not= 0 then
if place + x <= 100 then
place := place + x
end if
if place = 9 then
place := 34
elsif place = 40 then
place := 64
elsif place = 67 then
place := 86
elsif place = 99 then
place := 77
elsif place = 90 then
place := 48
elsif place = 54 then
place := 19
end if
put "You are now on square ", place
end if
exit when x = 0 or place = 100
end loop
if place = 100 then
put "You Win!"
else
put "You Quit!"
end if

74 changes: 74 additions & 0 deletions 2003/Junior/J4.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
% CCC 2003
% Problem J4/S2: Poetry
%
% get the last syllable of 4 lines and determine type of rhyme.
% relatively simple string processing, including converting
% to lower so that case is not an issue
%
% file I/O
% number of 4 line poems given, followed by those 4 sets of lines
% ouptut is a single word description of each group of 4 lines.

var fi, fo : int
var n : int
var a, b, c, d : string

%returns the last syllable: sequence from the last vowel to end
% or the entire last word if not vowel in that last word.

function lastSyllable (s : string) : string
var i : int
var vowels : string := "aeiou "
i := length (s)
loop
exit when i = 0 or index (vowels, s (i)) > 0
i := i - 1
end loop
if i = 0 or s (i) = " " then
result s (i + 1 .. *)
else
result s (i .. *)
end if
end lastSyllable

% converts the string to lower case
function toLower (s : string) : string
var t : string := ""
for i : 1 .. length (s)
if s (i) < "A" or s (i) > "Z" then
t := t + s (i)
else
t := t + chr (ord (s (i)) - ord ('A') + ord ('a'))
end if
end for
result t
end toLower

open : fi, "poetry1.in", get
open : fo, "poetry1.out", put

get : fi, n
for i : 1 .. n
get : fi, a : *
get : fi, b : *
get : fi, c : *
get : fi, d : *
a := lastSyllable (toLower (a))
b := lastSyllable (toLower (b))
c := lastSyllable (toLower (c))
d := lastSyllable (toLower (d))
if a = b and b = c and c = d then
put : fo, "perfect"
elsif a = b and c = d then
put : fo, "even"
elsif a = c and b = d then
put : fo, "cross"
elsif a = d and b = c then
put : fo, "shell"
else
put : fo, "free"
end if
end for
close : fi
close : fo

118 changes: 118 additions & 0 deletions 2003/Junior/J5.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// CCC 2003
// Problem S3J5 Floor Plan
//
// given a grid with walls and an amount of flooring,
// flooring the larger rooms first, determine the flooring reamining
// and the number of rooms floored.
//
// 1. read the floor plan (rooms are 0, walls are -1)
// 2. use recusion to determine which squares belong to a single room
// (number the first room 1, second 2, etc.)
// 3. determine the size of each room (add up all the 1's, 2's etc.)
// 4. find largest room, and floor it, set its value to -1, repeat!

// file input: amount of flooring, row, col and grid given
// output the # rooms floored and the remianing flooring

import java.awt.*;
import hsa.*;

public class S3J5Floor
{
static Console cc;
static int [] [] house;
static int r, c;

public static void main (String [] args)
{
cc = new Console ();

String line;
int n, k;
int [] room;
int count, largest;
boolean done;

TextInputFile fi = new TextInputFile ("floor5.in");
TextOutputFile fo = new TextOutputFile ("floor5.out");

// read grid
// put -1 for walls, 0 for room
n = fi.readInt ();
r = fi.readInt ();
c = fi.readInt ();
house = new int [r] [c];
for (int i = 0 ; i < r ; i++)
{
line = fi.readLine ();
for (int j = 0 ; j < c ; j++)
if (line.charAt (j) == 'I')
house [i] [j] = -1;
else
house [i] [j] = 0;
}

//number the rooms
k = 1;
for (int i = 0 ; i < r ; i++)
for (int j = 0 ; j < c ; j++)
if (house [i] [j] == 0)
{
check (i, j, k);
k++;
}

// determine area of rooms
room = new int [500];
for (int i = 0 ; i < r ; i++)
for (int j = 0 ; j < c ; j++)
if (house [i] [j] > 0)
room [house [i] [j]]++;

// get next largest room and floor it
count = 0;
done = false;
while (!done && n > 0)
{
largest = 0;
for (int i = 0 ; i < 500 ; i++)
if (room [i] > room [largest])
largest = i;
if (room [largest] > 0)
{
if (room [largest] <= n)
{
n = n - room [largest];
room [largest] = -1;
count++;
}
else
done = true;
}
else
done = true;
}

fo.println (count + " rooms, " + n + " square metre(s) left over");

fi.close ();
fo.close ();
}


// sets the house grid are current location to k, and recursively
// sets all connected square to k as well.
public static void check (int i, int j, int k)
{
if (i >= 0 && i < r && j >= 0 && j < c && house [i] [j] == 0)
{
house [i] [j] = k;
check (i - 1, j, k);
check (i + 1, j, k);
check (i, j + 1, k);
check (i, j - 1, k);
}
}
}


40 changes: 40 additions & 0 deletions 2003/Senior/S1.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
% CCC 2003
% Problem J3/S1: Snakes and Ladders
%
% simple dice roll input, move the piece. watch for 3 snakes and 3 ladders
% also watch for going over 100. Declare if win or quit.
%
% keyboard and screen I/O

var place, x : int
place := 1
loop
put "Enter sum of dice:"
get x
if x not= 0 then
if place + x <= 100 then
place := place + x
end if
if place = 9 then
place := 34
elsif place = 40 then
place := 64
elsif place = 67 then
place := 86
elsif place = 99 then
place := 77
elsif place = 90 then
place := 48
elsif place = 54 then
place := 19
end if
put "You are now on square ", place
end if
exit when x = 0 or place = 100
end loop
if place = 100 then
put "You Win!"
else
put "You Quit!"
end if

Loading

0 comments on commit cb51239

Please sign in to comment.