Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 CCC Prep -- '98
Index -> Contests
Goto page 1, 2  Next
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Hikaru79




PostPosted: 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? Smile 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.

Problem 1 -- Censors
Problem 2 -- Cross-Number Puzzle
Problem 3 -- Mars Rover
Problem 4 -- Lottery
[url=http://contest-cemc.uwaterloo.ca/ccc/1998/1e-prob.html"]Problem 5 -- Passage[/url]
Sponsor
Sponsor
Sponsor
sponsor
zylum




PostPosted: Mon Jan 23, 2006 9:50 am   Post subject: (No subject)

Java:
//CCC '98 Senior Problem 1
//Author: Michael Lucarz aka zylum

import java.io.*;
import java.util.*;
class Censor {
  public static void main(String[] args) {
   
    try {
        BufferedReader in = new BufferedReader(new FileReader("censor.in"));
        BufferedWriter out = new BufferedWriter(new FileWriter("censor.out"));
       
        int n = Integer.parseInt(in.readLine());
       
        for (int i = 0; i < n; i++) out.write(in.readLine().replaceAll("\\b\\p{Alpha}{4}\\b", "****") + (i < n-1 ? "\n\n" : ""));
       
        in.close(); out.close();
    } catch (IOException e) {}
   
  }
}
rizzix




PostPosted: Mon Jan 23, 2006 10:31 am   Post subject: (No subject)

clean it up...
Java:
import java.io.*;
import java.util.*;

class Censor {
  public static void main(String[] args) {

    try {
        BufferedReader in = new BufferedReader(
            new FileReader("censor.in")
        );
        PrintWriter out = new PrintWriter(
            new BufferedWriter(new FileWriter("censor.out"))
        );

        String line = in.readLine();

        while ((line = in.readLine()) != null)
            out.println(
                line.replaceAll("\\b\\p{Alpha}{4}\\b", "****")
            );

        in.close(); out.close();
    } catch (IOException e) {
        System.out.println(e);
    }
   
  }
}
Andy




PostPosted: Mon Jan 23, 2006 11:12 am   Post subject: (No subject)

c++:

//CCC '98 Senior Problem 2
//Author: Andy
#include <fstream>
#include <cmath>

using namespace std;

bool checkMersenne(int a)
{
        for (int i = 3; i <= sqrt(a); i+=2)
                if (a / i == (double)(a)/i)
                        return false;

        return true;
}

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

        return 0;
}


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 Laughing
MysticVegeta




PostPosted: 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




PostPosted: Mon Jan 23, 2006 11:39 am   Post subject: (No subject)

c++:
if (a / i == (double)(a)/i)
tsk tsk..
Andy




PostPosted: Mon Jan 23, 2006 11:41 am   Post subject: (No subject)

lol im lazy, sue me
zylum




PostPosted: 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 {
  public static void main (String[] args) {
 
    try {
        BufferedWriter out = new BufferedWriter(new FileWriter("perfect.out"));
       
        for (int i = 1000; i < 10000; i++) {
         
          int sum = 0;
         
          for (int j = 1; j <= Math.ceil(i / 2.0); j++) if (i % j == 0) sum += j;
         
          if (sum == i) out.write(i+"\n");
         
        }
       
        out.close();
    } catch (IOException e) {}
   
  }
}

//Part 2:

import java.io.*;
class Cube {
  public static void main (String[] args) {
 
    try {
        BufferedWriter out = new BufferedWriter(new FileWriter("cube.out"));
       
        for (int i = 100; i < 1000; i++) {
         
          int sum = 0;
         
          for (int j = 0; j < Math.log(i)/Math.log(10); j++) sum += Math.pow((int)(i / Math.pow(10, j)) % 10, 3);
         
          if (sum == i) out.write(i+"\n");
         
        }
       
        out.close();
    } catch (IOException e) {}
   
  }
}
Sponsor
Sponsor
Sponsor
sponsor
Andy




PostPosted: Mon Jan 23, 2006 11:48 am   Post subject: (No subject)

ouch zylum.. ur perfect number part is going to be slow
zylum




PostPosted: Mon Jan 23, 2006 11:59 am   Post subject: (No subject)

meh, runs in about a second on the computer im on.
Andy




PostPosted: 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 Laughing
MysticVegeta




PostPosted: 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




PostPosted: Mon Jan 23, 2006 3:47 pm   Post subject: (No subject)

Java:
//CCC '98 Senior Problem 5
//Author: Michael Lucarz aka zylum

import java.io.*;
class Passage {
       
        public static void main (String[] args) {
               
                try {
               
                        BufferedReader in = new BufferedReader(new FileReader("passage.in"));
                        BufferedWriter out = new BufferedWriter(new FileWriter("passage.out"));
                       
                        int cases = Integer.parseInt(in.readLine());
                       
                        for (int c = 0; c < cases; c++) {
                       
                                int n = Integer.parseInt(in.readLine());
                                int[][] map = new int[n][n];
                                for (int i = 0; i < n; i++)
                                        for (int j = 0; j < n; j++)
                                                map[i][j] = Integer.parseInt(in.readLine());
                               
                                int[][] matrix = new int[n*n][n*n];
                                for (int i = 0; i < n; i++)
                                        for (int j = 0; j < n; j++)
                                                for (int k = 0; k < n; k++)
                                                        for (int l = 0; l < n; l++)
                                                                if (Math.abs(i-k)+Math.abs(j-l) == 1)
                                                                        if (Math.abs(map[k][l] - map[i][j]) <= 2)
                                                                                matrix[i*n+j][k*n+l] = map[i][j] > map[0][0] || map[k][l] > map[0][0] ? 1 : 0;
                                                                        else matrix[i*n+j][k*n+l] = n*n+1;
                                                                else matrix[i*n+j][k*n+l] = n*n+1;
                                                                       
                               
                                for (int k = 0; k < n*n; k++)
                                        for (int i = 0; i < n*n; i++)
                                                for (int j = 0; j < n*n; j++)
                                                        matrix[i][j] = Math.min(matrix[i][j], matrix[i][k] + matrix[k][j]);     
                                                       
                                if (matrix[0][n*n-1] == n*n+1) out.write("CANNOT MAKE THE TRIP\n");
                                else out.write(matrix[0][n*n-1]+"\n");
                       
                        }
                       
                        in.close(); out.close();
                       
                } catch (IOException e) {}
               
        }
       
}
zylum




PostPosted: Mon Jan 23, 2006 3:56 pm   Post subject: (No subject)

MysticVegeta:

I challenge your code

code:
1
1 X 2 + 3 X 4
MysticVegeta




PostPosted: Mon Jan 23, 2006 4:12 pm   Post subject: (No subject)

Sorry. Code updated. Thanks. Problem 3 source coming soon as soon as i finish my science studying for exam 2morrow.
Display posts from previous:   
   Index -> Contests
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 2  [ 17 Posts ]
Goto page 1, 2  Next
Jump to:   


Style:  
Search: