Posted: Mon Jan 23, 2006 8:22 am Post subject: CCC Prep -- '98
This year's CCC is looming near, and what better way to prepare than reviewing previous years' CCC's? I'll just post the questions to 1998's senior contest, and let's try to get together a large selection of different solutions. Remember, the CCC allows any language to be used, so if you consider a question to be relatively simple, or you've done it before, challenge yourself by doing it in a language you're still just learning.
Posted: Mon Jan 23, 2006 11:12 am Post subject: (No subject)
c++:
//CCC '98 Senior Problem 2 //Author: Andy #include <fstream> #include <cmath>
usingnamespace std;
bool checkMersenne(int a) { for(int i = 3; i <= sqrt(a); i+=2) if(a / i == (double)(a)/i) returnfalse;
returntrue;
}
int main() {
ofstream fout("perfect.out");
for(int i = 1; (pow(2, i ) - 1)*pow(2, i - 1) < 10000; i++) if(((pow(2, i) - 1) * pow(2, i - 1) >= 1000) && checkMersenne(pow(2, i) - 1))
fout<< (pow(2, i) - 1) * pow(2, i - 1) << endl;
fout.close();
fout.open("cube.out");
for(int a = 1; a < 10; a++) for(int b = 0; b < 10; b++) for(int c = 0; c < 10; c++) if((100 * a + 10 * b + c) == (pow(a, 3) + pow(b, 3) + pow(c, 3)))
fout<< (100*a + 10*b + c) << endl;
fout.close();
return0;
}
8 1 2 8
1 2
8 1 1 8
3 7 1
for the cube digit one i got lazy, but i dont see any better way to do the first one.. the power of mersenne primes
MysticVegeta
Posted: Mon Jan 23, 2006 11:33 am Post subject: (No subject)
Turing source :
code:
%AUTHOR : MYSTICVEGETA aka Tan
%CCC PROBLEM 1.
var fi, fo : int
open : fi, "DATA1.txt", get
open : fo, "OUT1.txt", put
var n : int
var line : string
var fArray : flexible array 1 .. 0 of string
get : fi, n
proc splitToArray (l : string)
var sen := l
loop
var ind := index (sen, " ")
new fArray, upper (fArray) + 1
fArray (upper (fArray)) := sen (1 .. ind - 1)
sen := sen (ind + 1 .. *)
exit when index (sen, " ") = 0
end loop
new fArray, upper (fArray) + 1
fArray (upper (fArray)) := sen (1 .. *)
end splitToArray
for x : 1 .. n
get : fi, line : *
splitToArray (line (1 .. *))
line := ""
for i : 1 .. upper (fArray)
if length (fArray (i)) = 4 then
fArray (i) := "****"
end if
line += fArray (i) + " "
end for
put : fo, line (1 .. * -1)
free fArray
end for
close (fi)
close (fo)
rizzix
Posted: Mon Jan 23, 2006 11:39 am Post subject: (No subject)
c++:
if(a / i == (double)(a)/i)
tsk tsk..
Andy
Posted: Mon Jan 23, 2006 11:41 am Post subject: (No subject)
lol im lazy, sue me
zylum
Posted: Mon Jan 23, 2006 11:46 am Post subject: (No subject)
Java:
//CCC '98 Senior Problem 2 //Author: Michael Lucarz aka zylum
//Part 1:
import java.io.*; class Perfect { publicstaticvoid main (String[] args){
Posted: Mon Jan 23, 2006 11:48 am Post subject: (No subject)
ouch zylum.. ur perfect number part is going to be slow
zylum
Posted: Mon Jan 23, 2006 11:59 am Post subject: (No subject)
meh, runs in about a second on the computer im on.
Andy
Posted: Mon Jan 23, 2006 12:08 pm Post subject: (No subject)
i've heard that the ppl at ccc test your code with old p2 celerons
MysticVegeta
Posted: Mon Jan 23, 2006 3:42 pm Post subject: (No subject)
Turing Source for problem 4:
code:
%AUTHOR : MYSTICVEGETA aka Tan
%CCC PROBLEM 4
var fi, fo : int
open : fi, "DATA2.txt", get
open : fo, "OUT2.txt", put
var n : int
var line : string
var fArray : flexible array 1 .. 0 of string
var main : flexible array 1 .. 0 of string
get : fi, n
proc splitToArray (l : string)
var sen := l
loop
var ind := index (sen, " ")
new fArray, upper (fArray) + 1
fArray (upper (fArray)) := sen (1 .. ind - 1)
sen := sen (ind + 1 .. *)
exit when index (sen, " ") = 0
end loop
new fArray, upper (fArray) + 1
fArray (upper (fArray)) := sen (1 .. *)
end splitToArray
fcn mulOrDiv (ar : array 1 .. * of string) : boolean
for x : 1 .. upper (ar)
if (ar (x) = "X") or (ar (x) = "/") then
result true
end if
end for
result false
end mulOrDiv
fcn retCon (ar : array 1 .. * of string) : boolean
var p := 0
var s := "X/+-"
for x : 1 .. upper (ar)
if (index (s, ar (x)) > 0) then %(ar (x) (1) not= "(") and (ar (x) (*) not= ")") and (index (s, ar (x)) <= 0) then
p += 1
end if
end for
if p <= 1 then
result true
else
result false
end if
end retCon
proc solve (arr : array 1 .. * of string)
var pList := ""
if not (mulOrDiv (arr)) then
pList := "+-"
else
pList := "X/"
end if
% for x : 1 .. upper (arr)
% put arr (x) + " " ..
% end for
% put ""
% Input.Pause
for x : 1 .. upper (arr)
if index (pList, arr (x)) > 0 then
var stored := ""
stored := "(" + arr (x - 1) + " " + arr (x) + " " + arr (x + 1) + ")"
for y : 1 .. upper (arr)
if (y not= x) and (y not= x + 1) then
new main, upper (main) + 1
if (y = x - 1) then
main (upper (main)) := stored
else
main (upper (main)) := arr (y)
end if
end if
end for
exit
end if
end for
free fArray
for x : 1 .. upper (main)
new fArray, x
fArray (x) := main (x)
end for
end solve
for x : 1 .. n
get : fi, line : *
var ans := ""
splitToArray (line (1 .. *))
loop
solve (fArray)
free main
exit when retCon (fArray)
end loop
for i : 1 .. upper (fArray)
ans += fArray (i) + " "
end for
put : fo, ans (1 .. * -1)
free fArray
end for
zylum
Posted: Mon Jan 23, 2006 3:47 pm Post subject: (No subject)
Java:
//CCC '98 Senior Problem 5 //Author: Michael Lucarz aka zylum