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

Username:   Password: 
 RegisterRegister   
 Ccc 2008 #4
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
A.J




PostPosted: Sat Mar 08, 2008 4:24 pm   Post subject: Ccc 2008 #4

I wanted ask if people got an efficient solution for #4.
here is my solution :
code:

fcn work (var hand : array 1 .. 4 of int, num : int) : int
    if num = 1 then
        result hand (1)
    end if
    var score := 0
    var cur : int
    var temp : array 1 .. 4 of int
    for i : 1 .. num - 1
        for j : 1 .. 4
            for k : 1 .. 4
                temp (k) := hand (k)
            end for
            if j = 1 then
                temp (i) += temp (i + 1)
            end if
            if j = 2 then
                temp (i) -= temp (i + 1)
            end if
            if j = 3 then
                temp (i) *= temp (i + 1)
            end if
            if j = 4 then
                if temp (i + 1) ~= 0 and temp (i) mod temp (i + 1) = 0 then
                    temp (i) div= temp (i + 1)
                else
                    exit
                end if
            end if
            for k : i + 1 .. 3
                temp (k) := temp (k + 1)
            end for
            cur := work (temp, num - 1)
            if cur <= 24 and cur > score then
                score := cur
            end if
        end for
    end for
    result score
end work
var stream, n : int
open : stream, "s4.txt", get
get : stream, n
for A : 1 .. n
    var hand, newhand : array 1 .. 4 of int
    for i : 1 .. 4
        get : stream, hand (i)
    end for
    var ma := 0
    for i : 1 .. 4
        for j : 1 .. 4
            if j ~= i then
                for k : 1 .. 4
                    if k ~= j and k ~= i then
                        for l : 1 .. 4
                            if l ~= k and l ~= j and l ~= i then
                                if ma ~= 24 then
                                    newhand (i) := hand (1)
                                    newhand (j) := hand (2)
                                    newhand (k) := hand (3)
                                    newhand (l) := hand (4)
                                    var x := work (newhand, 4)
                                    if x > ma and x <= 24 then
                                        ma := x
                                    end if
                                end if
                            end if
                        end for
                    end if
                end for
            end if
        end for
    end for
    put ma
end for

it works for all test cases, but i saw a RPN stack method in the unofficial solutions page and i was wishing someone could explain that to me.

thx again,
A.J
Sponsor
Sponsor
Sponsor
sponsor
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 1 Posts ]
Jump to:   


Style:  
Search: