- Translate each rule into one explicit state update.
- Maintain the invariant after every processed item.
- Return the accumulated state once all relevant input has been handled.
Code notes
- 84 lines of Turing from the credited upstream file ccc96s2.t.
- The implementation keeps its working state in language-native values and containers.
- No explicit loop blocks detected.
Complexity
Count the number and nesting of passes over the input, then include the maintained containers in the memory estimate.
Check the problem constraints before deciding whether this complexity will pass.
Use this to learn the idea, then write your own version.
123 4567 891011 12var infile : string := "div.in"13var outfile : string := "div.out"14var fi, fo : int15var n : int16var line : string17var digit : int18var x : array 1 .. 50 of int19var xn : int20 21open : fi, infile, get22open : fo, outfile, put23 24get : fi, n25for i : 1 .. n26 27 28 get : fi, line29 xn := length (line)30 for k : 1 .. xn31 x (k) := strint (line (k))32 end for33 34 35 loop36 37 38 for k : 1 .. xn39 put : fo, x (k) ..40 put x (k) ..41 end for42 put : fo, ""43 put ""44 exit when xn <= 245 46 47 digit := x (xn)48 xn := xn - 149 for decreasing j : xn .. 150 if digit > x (j) then51 x (j) := x (j) + 1052 x (j - 1) := x (j - 1) - 153 end if54 x (j) := x (j) - digit55 digit := 056 end for57 58 59 if x (1) = 0 then60 xn := xn - 161 for k : 1 .. xn62 x (k) := x (k + 1)63 end for64 end if65 end loop66 if xn < 2 then67 put : fo, "The number ", line, " is not divisible by 11."68 put "The number ", line, " is not divisible by 11."69 elsif x (1) = x (2) then70 put : fo, "The number ", line, " is divisible by 11."71 put "The number ", line, " is divisible by 11."72 else73 put : fo, "The number ", line, " is not divisible by 11."74 put "The number ", line, " is not divisible by 11."75 end if76 put : fo, ""77 put ""78end for79 80close : fi81close : fo82 83 84