Skip to content

Commit

Permalink
Added 1996-2001 Solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
johnafish committed Feb 18, 2016
1 parent aca71be commit 81ffec7
Show file tree
Hide file tree
Showing 47 changed files with 4,438 additions and 0 deletions.
54 changes: 54 additions & 0 deletions 1996/P1.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
% CCC 1996
% problem 1: Deficient, Perfect and Abundant Numbers
%
% a perfect number = the sum of its proper divisors (1 thru < n)
% a Deficent number < the sum of its proper divisors (1 thru < n)
% an Abundant number > the sum of its proper divisors (1 thru < n)

% file handling is used, the number of numbers is given
% the numbers will be between 1 and 32500.

function sumFactors (x : int) : int
var sum : int
sum := 0
for f : 1 .. x - 1
if x mod f = 0 then
sum := sum + f
end if
end for
result sum
end sumFactors

var infile : string := "dpa.in"
var outfile : string := "dpa.out"
var fi, fo : int
var n : int
var x : int
var sum : int

open : fi, infile, get
open : fo, outfile, put

get : fi, n
for i : 1 .. n
get : fi, x
sum := sumFactors (x)
if sum < x then
put x, " is a deficient number."
put : fo, x, " is a deficient number."
elsif x = sum then
put x, " is a perfect number."
put : fo, x, " is a perfect number."
else
put x, " is an abundant number."
put : fo, x, " is an abundant number."
end if
end for

close : fi
close : fo





83 changes: 83 additions & 0 deletions 1996/P2.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
% CCC 1996
% problem 2 Divisibility by 11

% take the last digit off the number and subtract it from the shortened
% number (repeatedly) until the number is 2 digits.
% If n is divisible by 11 the original was divisible by 11.

% file i/o used.
% the first number is the number of numbers.
% numbers may be 50 digits long, no leading zeros.

var infile : string := "div.in"
var outfile : string := "div.out"
var fi, fo : int
var n : int
var line : string
var digit : int
var x : array 1 .. 50 of int
var xn : int

open : fi, infile, get
open : fo, outfile, put

get : fi, n
for i : 1 .. n

% read the line and convert to integer array
get : fi, line
xn := length (line)
for k : 1 .. xn
x (k) := strint (line (k))
end for

%while the array length is greater than 2
loop

% print the array
for k : 1 .. xn
put : fo, x (k) ..
put x (k) ..
end for
put : fo, ""
put ""
exit when xn <= 2

% do the subtraction: go left from end, borrowing if necessary
digit := x (xn)
xn := xn - 1
for decreasing j : xn .. 1
if digit > x (j) then
x (j) := x (j) + 10
x (j - 1) := x (j - 1) - 1
end if
x (j) := x (j) - digit
digit := 0
end for

% check / remove a leading zero
if x (1) = 0 then
xn := xn - 1
for k : 1 .. xn
x (k) := x (k + 1)
end for
end if
end loop
if xn < 2 then
put : fo, "The number ", line, " is not divisible by 11."
put "The number ", line, " is not divisible by 11."
elsif x (1) = x (2) then
put : fo, "The number ", line, " is divisible by 11."
put "The number ", line, " is divisible by 11."
else
put : fo, "The number ", line, " is not divisible by 11."
put "The number ", line, " is not divisible by 11."
end if
put : fo, ""
put ""
end for

close : fi
close : fo


75 changes: 75 additions & 0 deletions 1996/P3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// CCC 1996
// Problem C: Pattern Generator

// Given k and n, print all bit patterens of k 1's in bit strings of length n
// in descending order

// Eg: k = 2 n = 5
// 11000
// 10100
// 10010
// 10001
// 01100
// 01010
// 01001
// 00110
// 00101
// 00011

// There is "trick" to this:
// a. create the first string (easy)
// b. for ALL other strings find the LAST "10" and reverse it, AND
// reverse the right part of the sring AFTER the "10"
//
import java.awt.*;
import hsa.*;

public class P3bitpattern
{
static Console c; // The output console

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

TextInputFile fin = new TextInputFile ("pat.in");
TextOutputFile fout = new TextOutputFile ("pat.out");
String s;
StringBuffer b;
int number, i, k, n, x;

number = fin.readInt ();
for (int j = 0 ; j < number ; j++)
{

// read and create the original string
n = fin.readInt ();
k = fin.readInt ();
s = "";
for (i = 0 ; i < k ; i++)
s = s + "1";
for (; i < n ; i++)
s = s + "0";

// find the last "10", reverse that AND
// reverse the part to the right of it
x = s.lastIndexOf ("10");
fout.println ("The bit patterns are: ");
c.println ("The bit patterns are: ");
while (x >= 0)
{
fout.println (s);
c.println (s);
b = new StringBuffer (s.substring (x + 2));
s = s.substring (0, x) + "01" + b.reverse ();
x = s.lastIndexOf ("10");
}
c.println (s);
c.println ("\n");
fout.println (s);
fout.println ("\n");
}
fout.close ();
fin.close ();
}
}
142 changes: 142 additions & 0 deletions 1996/P4.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
% CCC 1996
% problem 4 Calculate as the Romans Do

% convert to and from Roman Numerals to do addition
% max number is 1000

% file i/o used.
% the first number is the number of numbers.
% expression is in the form "a+b=" where a and b are Roman Numerals

function toDecimal (s : string) : int
var t, v : int
var old : int := 100000
t := 0
for i : 1 .. length (s)
if s (i) = "I" then
v := 1
elsif s (i) = "V" then
v := 5
elsif s (i) = "X" then
v := 10
elsif s (i) = "L" then
v := 50
elsif s (i) = "C" then
v := 100
elsif s (i) = "D" then
v := 500
elsif s (i) = "M" then
v := 1000
end if
if v > old then
t := t + v - 2 * old
else
t := t + v
end if
old := v
end for
result t
end toDecimal

% very unFancy: brute force is easiest
function toRoman (xx : int) : string
var x : int := xx
var d : int
var s : string
s := ""
d := x div 100
x := x mod 100
if d = 1 then
s := s + "C"
elsif d = 2 then
s := s + "CC"
elsif d = 3 then
s := s + "CCC"
elsif d = 4 then
s := s + "CD"
elsif d = 5 then
s := s + "D"
elsif d = 6 then
s := s + "DC"
elsif d = 7 then
s := s + "DCC"
elsif d = 8 then
s := s + "DCCC"
elsif d = 9 then
s := s + "CM"
end if
d := x div 10
x := x mod 10
if d = 1 then
s := s + "X"
elsif d = 2 then
s := s + "XX"
elsif d = 3 then
s := s + "XXX"
elsif d = 4 then
s := s + "XL"
elsif d = 5 then
s := s + "L"
elsif d = 6 then
s := s + "LX"
elsif d = 7 then
s := s + "LXX"
elsif d = 8 then
s := s + "LXXX"
elsif d = 9 then
s := s + "XC"
end if
d := x
if d = 1 then
s := s + "I"
elsif d = 2 then
s := s + "II"
elsif d = 3 then
s := s + "III"
elsif d = 4 then
s := s + "IV"
elsif d = 5 then
s := s + "V"
elsif d = 6 then
s := s + "VI"
elsif d = 7 then
s := s + "VII"
elsif d = 8 then
s := s + "VIII"
elsif d = 9 then
s := s + "IX"
end if
result s
end toRoman


var infile : string := "rom.in"
var outfile : string := "rom.out"
var fi, fo : int
var n : int
var line : string
var plus, answer : int

open : fi, infile, get
open : fo, outfile, put

get : fi, n
for i : 1 .. n
get : fi, line
plus := index (line, "+")
answer := toDecimal (line (1 .. plus - 1)) + toDecimal (line (plus +
1 .. length (line) - 1))
if answer > 1000 then
put : fo, line, "CONCORDIA CUM VERITATE"
put line, "CONCORDIA CUM VERITATE"
else
put : fo, line, toRoman (answer)
put line, toRoman (answer)
end if
put : fo, ""
put ""
end for

close : fi
close : fo

Loading

0 comments on commit 81ffec7

Please sign in to comment.