Problem solution · Turing

CCC 1997 P5 - Long Division

CCC 1997 P5 - Long Division: a Turing solution using direct simulation. Learn the idea, check the complexity, and read the full code, with credit to CCCSolutions.

Technique
Direct simulation
Source
CCCSolutions
Length
141 lines
Start with the idea.

Try the problem first. If you get stuck, read the approach below, then write your own solution. The full code is at the bottom.

Approach

Direct simulation

For CCC 1997 P5 - Long Division, the implementation follows the problem’s operations directly while maintaining only the state needed for the next decision.

  1. Translate each rule into one explicit state update.
  2. Maintain the invariant after every processed item.
  3. Return the accumulated state once all relevant input has been handled.

Code notes

  • 141 lines of Turing from the credited upstream file ccc97s5.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.

Source

Code and credit

This code comes from CCCSolutions by CCCSolutions contributors · Milliken Mills High School and is used under the MIT licence.

Full codeCCC 1997 P5 - Long Division · TuringTuring
Use this to learn the idea, then write your own version.
% CCC 1997% problem E: Long Division%% this is essentially long subtraction! Must be able to do mulitdigit (array)% subtraction and recognize if negatives are produced. UGLY! % file handling is used, the number of test cases is given% then each case is two lines, dividend and divisor. var infile : string := "div.in"var outfile : string := "div.out"var fi, fo : intvar n, ndvd, ndvs, nquo : intvar dvd : array 1 .. 100 of intvar dvs : array 1 .. 100 of intvar quo : array 1 .. 100 of intvar t : array 1 .. 100 of intvar offset, k, m : intvar can : booleanvar line : stringvar key : string (1) open : fi, infile, getopen : fo, outfile, put get : fi, nfor i : 1 .. n     % get the dividend and divisor, convert to integer arrays.    get : fi, line    for j : 1 .. length (line)        dvd (j) := strint (line (j))    end for    ndvd := length (line)    get : fi, line    for j : 1 .. length (line)        dvs (j) := strint (line (j))    end for            ndvs := length (line)    nquo := 0    offset := 0    loop        exit when ndvs > ndvd - offset        nquo := nquo + 1        quo (nquo) := 0         loop            % save dividend            for j : 1 .. ndvd                t (j) := dvd (j)            end for             % do the division            can := true            k := ndvs            m := ndvs + offset            loop                exit when m = 0                if k >= 1 then                    if dvd (m) >= dvs (k) then                        dvd (m) := dvd (m) - dvs (k)                    else                        dvd (m) := (dvd (m) + 10) - dvs (k)                        if m = 1 then                            can := false                        else                            dvd (m - 1) := dvd (m - 1) - 1                        end if                    end if                else                    if dvd (m) >= 0 then                        dvd (m) := dvd (m)                    else                        dvd (m) := (dvd (m) + 10)                        if m = 1 then                            can := false                        else                            dvd (m - 1) := dvd (m - 1) - 1                        end if                    end if                end if                k := k - 1                m := m - 1            end loop            if can then                quo (nquo) := quo (nquo) + 1            else                % restore dividend                for j : 1 .. ndvd                    dvd (j) := t (j)                end for            end if            exit when can = false        end loop        offset := offset + 1    end loop     %print quotient, eliminating leading zeros    if nquo = 0 then        put : fo, "0"        put "0"    else        k := 1        loop            exit when k >= nquo or quo (k) not= 0            k := k + 1        end loop        loop            exit when k > nquo            put : fo, quo (k) ..            put quo (k) ..            k := k + 1        end loop        put : fo, ""        put ""    end if        %print remainder(dividend), eliminating leading zeros    k := 1    loop        exit when k >= ndvd or dvd (k) not= 0        k := k + 1    end loop    loop        exit when k > ndvd        put : fo, dvd (k) ..        put dvd (k) ..        k := k + 1    end loop    put : fo, ""    put : fo, ""    put ""    put ""end for close : ficlose : fo  

Did this explanation save you time? I'm a Grade 11 student building this free library to make difficult algorithms easier to understand.

Buy me a coffee ↗