Skip to content

Commit

Permalink
Added 2005 Solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
johnafish committed Feb 18, 2016
1 parent cff212e commit 067786e
Show file tree
Hide file tree
Showing 14 changed files with 906 additions and 0 deletions.
37 changes: 37 additions & 0 deletions 2005/Junior/J1.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
% CCC 2005 Junior problem 1
% The Cell Sell
%
% comparing two cellphone plans, with various rates depending on time
% straight forward arithmetic and decision structures

var day, night, weekend : int
var planA, planB : real
put "Number of daytime minutes?"
get day
put "Number of evening minutes?"
get night
put "Number of weekend minutes?"
get weekend
if day > 100 then
planA := (day - 100) * .25 + night * .15 + .20 * weekend
else
planA := night * .15 + .20 * weekend
end if
if day > 250 then
planB := (day - 250) * .45 + night * .35 + .25 * weekend
else
planB := night * .35 + .25 * weekend
end if
put "Plan A costs ", planA : 0 : 2
put "Plan B costs ", planB : 0 : 2

% abs needed because turing stores reals with a potential small error
if abs (planA - planB) < 0.00005 then
put "Plan A and B are the same price."
elsif planA > planB then
put "Plan B is the cheapest."
else
put "Plan A is the cheapest."
end if


31 changes: 31 additions & 0 deletions 2005/Junior/J2.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
% CCC 2005 Junior problem 2
% RSA numbers
%
% simple arithmetic pgm with structures

function RSA (n : int) : boolean
var f : int := 2
var count : int := 1
loop
exit when f > n or count > 4
if n mod f = 0 then
count := count + 1
end if
f := f + 1
end loop
result count = 4
end RSA

var x, y, c : int
put "Enter lower limit of range"
get x
put "Enter upper limit of range"
get y
c := 0
for i : x .. y
if RSA (i) then
c := c + 1
end if
end for
put "The number of RSA numbers between ", x, " and ", y, " is ", c

26 changes: 26 additions & 0 deletions 2005/Junior/J3.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
% CCC 2005 Junior problem 3
% Returning Home
%
% simple array and string processing

var out : array 1 .. 10 of string
var n : int
var s : string
var hold : string
n := 0
hold := "into your HOME."
loop
get s
n := n + 1
if s = "L" then
out (n) := "Turn RIGHT " + hold
else
out (n) := "Turn LEFT " + hold
end if
get s
hold := "onto " + s + " Street."
exit when s = "SCHOOL"
end loop
for decreasing i : n .. 1
put out (i)
end for
120 changes: 120 additions & 0 deletions 2005/Junior/J4.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
% CCC 2005 Junior problem 4
% Cross Spiral
%
% a 2D array problem, with orientation paramount in a spiral
% cross shape. Not too ugly, but...
%
% make the grid larger than necessary to give borders,
% so bounds checking is not needed

var g : array 0 .. 21, 0 .. 21 of boolean
var w, h, cw, ch : int
var c, r, direction : int
var steps : int
var moving : boolean

get w, h, cw, ch, steps
% initialize grid
for i : 0 .. 21
for j : 0 .. 21
if i >= 1 and i <= h and j >= 1 and j <= w and
not ( (i <= ch and (j <= cw or j > w - cw)) or
(i > h - ch and (j <= cw or j > w - cw))) then
g (i, j) := true
else
g (i, j) := false
end if
end for
end for

%- testing
%- for i : 0 .. 21
%- for j : 0 .. 21
%- if g (i, j) then
%- put " " ..
%- else
%- put "*" ..
%- end if
%- end for
%- put ""
%- end for
%- var k : string (1)
%- getch (k)

c := cw + 1
r := 1
direction := 0
for i : 1 .. steps
g (r, c) := false
moving := true
if direction = 0 then
if g (r - 1, c) then
r := r - 1
direction := 90
elsif g (r, c + 1) then
c := c + 1
direction := 0
elsif g (r + 1, c) then
r := r + 1
direction := 270
elsif g (r, c - 1) then
c := c - 1
direction := 180
else
moving := false
end if
elsif direction = 90 then
if g (r, c - 1) then
c := c - 1
direction := 180
elsif g (r - 1, c) then
r := r - 1
direction := 90
elsif g (r, c + 1) then
c := c + 1
direction := 0
elsif g (r + 1, c) then
r := r + 1
direction := 270
else
moving := false
end if
elsif direction = 180 then
if g (r + 1, c) then
r := r + 1
direction := 270
elsif g (r, c - 1) then
c := c - 1
direction := 180
elsif g (r - 1, c) then
r := r - 1
direction := 90
elsif g (r, c + 1) then
c := c + 1
direction := 0
else
moving := false
end if
elsif direction = 270 then
if g (r, c + 1) then
c := c + 1
direction := 0
elsif g (r + 1, c) then
r := r + 1
direction := 270
elsif g (r, c - 1) then
c := c - 1
direction := 180
elsif g (r - 1, c) then
r := r - 1
direction := 90
else
moving := false
end if
end if
exit when not moving

end for
put c
put r

56 changes: 56 additions & 0 deletions 2005/Junior/J5_1.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
% CCC 2005 Junior problem 5
% Bananas
%
% this is an exercise in recursion.
% it involved a pair of mutually recursive methods.
%
% This is quite beyond all but the best "junior" programmers
% and would stump many "senior" high school programmers. :-)

forward function monkeyWord (s : string) : boolean

% an a-word is either:
% the letter A OR
% the letter B + monkey language word + S
function aWord (s : string) : boolean
if s = "A" then
result true
elsif length (s) >= 3 and s (1) = "B" and
monkeyWord (s (2 .. * - 1)) and s (*) = "S" then
result true
else
result false
end if
end aWord

% an monkey language word is either:
% an a-word or
% an a-word + N + monkey language word
body monkeyWord
if aWord (s) then
result true
else
% try all combos for the more complex version
var found : boolean := false
for i : 2 .. length (s) - 1
found := found or
(aWord (s (1 .. i - 1)) and
s (i) = "N" and
monkeyWord (s (i + 1 .. *)))
end for
result found
end if
end monkeyWord


var s : string
loop
get s
exit when s = "X"
if monkeyWord (s) then
put "YES"
else
put "NO"
end if
end loop

42 changes: 42 additions & 0 deletions 2005/Junior/J5_2.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
% CCC 2005 Junior problem 5
% Bananas
%
% this is a non-recursive version.
% simply go through the string replacing all "ANA" or "BAS"
% with "A". A monkey word will eventually be just "A".
%
% this approach was developed by Jason Jackson of Aurora High School
%

function monkeyWord (t : string) : boolean
var s : string := t
var x : int
loop
exit when index (s, "ANA") = 0 and index (s, "BAS") = 0
loop
exit when index (s, "ANA") = 0
x := index (s, "ANA")
s := s (1 .. x - 1) + "A" + s (x + 3 .. *)
end loop
loop
exit when index (s, "BAS") = 0
x := index (s, "BAS")
s := s (1 .. x - 1) + "A" + s (x + 3 .. *)
end loop
end loop
result s = "A"
end monkeyWord

var s : string
loop
get s
exit when s = "X"
if monkeyWord (s) then
put "YES"
else
put "NO"
end if
end loop



60 changes: 60 additions & 0 deletions 2005/Senior/S1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// The "CCC2005s1snowcalls" class.
//
// Basic string handling: that and basic structures

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

public class CCC2005s1SnowCalls
{
static Console c;

public static void main (String[] args)
{
c = new Console ();
TextInputFile f = new TextInputFile ("s1.in");
int n;
String s;

n = f.readInt ();
for (int i = 1 ; i <= n ; i++)
c.println (convert (f.readLine ()));
}

// no checks are made on s as the problem states it
// represents a valid number
public static String convert (String s)
{
String out;
int i;
i = 0;
out = "";
while (out.length () < 12)
{
if (s.charAt (i) == '1')
out = out + "1";
else if (s.charAt (i) == '2' || s.charAt (i) == 'A' || s.charAt (i) == 'B' || s.charAt (i) == 'C')
out = out + "2";
else if (s.charAt (i) == '3' || s.charAt (i) == 'D' || s.charAt (i) == 'E' || s.charAt (i) == 'F')
out = out + "3";
else if (s.charAt (i) == '4' || s.charAt (i) == 'G' || s.charAt (i) == 'H' || s.charAt (i) == 'I')
out = out + "4";
else if (s.charAt (i) == '5' || s.charAt (i) == 'J' || s.charAt (i) == 'K' || s.charAt (i) == 'L')
out = out + "5";
else if (s.charAt (i) == '6' || s.charAt (i) == 'M' || s.charAt (i) == 'N' || s.charAt (i) == 'O')
out = out + "6";
else if (s.charAt (i) == '7' || s.charAt (i) == 'P' || s.charAt (i) == 'Q' || s.charAt (i) == 'R' || s.charAt (i) == 'S')
out = out + "7";
else if (s.charAt (i) == '8' || s.charAt (i) == 'T' || s.charAt (i) == 'U' || s.charAt (i) == 'V')
out = out + "8";
else if (s.charAt (i) == '9' || s.charAt (i) == 'W' || s.charAt (i) == 'X' || s.charAt (i) == 'Y' || s.charAt (i) == 'Z')
out = out + "9";
else if (s.charAt (i) == '0')
out = out + "0";
i++;
if (out.length () == 3 || out.length () == 7)
out = out + "-";
}
return out;
}
}
Loading

0 comments on commit 067786e

Please sign in to comment.