% Canadian Computing Competition % Example program to demonstrate input and output and time limit. % Programming Language: Java % Specification: % Write a program that reads several positive integers, one per % line. For each integer n, output the number of orderings % possible for a set of n distinct values. n will not exceed 11. % The last line of input is indicated by 0. % Sample Input: % 1 % 11 % 0 % Output for Sample Input: % 1 % 39916800 % Implementation: % The answer is n! (n factorial) which is easily computed in n steps. % But this program does it the hard way. It uses a recursive function % to enumerate and count all the possible orderings. % How to run the program: % The program reads from "standard input" and writes to "standard output." % Run-time: % Please time the execution time of this program on your computer, % using the sample input. This time is the maximum that should be % allowed for any CCC program. % On a 350 MHz processor, this program runs in about 15 seconds. % (Your results will vary depending on processor, compiler, and % compiler options.) function countfact(var s : array 1 .. 11 of int, n, total : int) : int var r, t : int := 0 if n = 1 then % uncomment to print out each ordering % for j : 1 .. total % put s(j) .. % put " " .. % end for % put " " r := 1 else for i : 1 .. n t := s(i) s(i) := s(n - 1) s(n - 1) := t r := r + countfact(s, n-1, total) s(n - 1) := s(i) s(i) := t end for end if result r end countfact var n : int := 1 var set1 : array 1 .. 11 of int % Initialize the set of values for i : 1 .. 11 set1(i) := i end for loop exit when n <= 0 get n % 0 would do nothing, and -1 is nothing to read if n > 0 then put countfact(set1, n, n) else n := 0 end if end loop