' Canadian Computing Competition ' Example program to demonstrate input and output and time limit. ' Programming Language: Visual Basic ' 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 will be indicated with 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. ' Author's Note: ' As far as I know, versions of Visual Basic before .NET do not have ' any notion of a console for input. There is no such thing as ' standard input, and thus no way to redirect input from a file. ' For this example, I used an "input box" (pop-up window) to get the ' input 'from the user. These is an "immediate" window, which is like ' standard output. This is where the "Debug.Print" statements appear. ' I don't know how to redirect it to a file, though. -- Graeme ' How to run the program: ' Copy and paste the code into a new "Visual Basic form". ' Run-time: ' For n=10, this program takes about 30 seconds on a P4 2.0GHz machine Private Sub Form_Load() Dim i, n, s(11) As Long 'You need long since the integer data type is only 16-bits For i = 1 To 11 s(i) = i Next i Do While (True) n = InputBox("Enter number:") If n = 0 Then Exit Do Debug.Print countfact(s, n, n) Loop End End Sub Function countfact(s, k, n) Dim i, j, r As Long r = 0 If k = 0 Then 'uncomment to print out the orderings 'For j = 1 To n ' Debug.Print s(j); 'Next j 'Debug.Print r = 1 Else For i = 1 To k Dim t As Long t = s(i) s(i) = s(k) s(k) = t r = r + countfact(s, k - 1, n) s(k) = s(i) s(i) = t Next i End If countfact = r End Function