From 2f674dc80f0382f1c3178f435714960734dc9d3c Mon Sep 17 00:00:00 2001 From: Nguyễn Gia Phong Date: Sat, 6 Jun 2020 21:33:13 +0700 Subject: Reorganize stuff from secondary school --- 2ndary/THT/B/QG-2014/dic.pp | 96 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 2ndary/THT/B/QG-2014/dic.pp (limited to '2ndary/THT/B/QG-2014/dic.pp') diff --git a/2ndary/THT/B/QG-2014/dic.pp b/2ndary/THT/B/QG-2014/dic.pp new file mode 100644 index 0000000..716249d --- /dev/null +++ b/2ndary/THT/B/QG-2014/dic.pp @@ -0,0 +1,96 @@ +unit dic; +interface + function count_char(c: char): longint; + function get_char_at_pos(x: longint): char; + procedure answer(s: string); + +implementation +var + secret_word: string; + words: array [1..1000000] of string; + total_cost, n: longint; + +procedure answer(s: string); +begin + if s = secret_word then + begin + writeln('Chuc mung ban da tim ra dap an chinh xac la "', s, '"'); + writeln('Chi phi ban da su dung la ', total_cost); + end + else + begin + writeln('Dap an ban dua ra la "', s, '"'); + writeln('Dap an chinh xac la "', secret_word, '"'); + end; + halt; +end; + +procedure cost_limit_exceed; +begin + writeln('Chi phi ban da su dung vuot qua chi phi toi da cho phep'); + halt; +end; + +function count_char(c: char): longint; +var + i, res: longint; +begin + total_cost := total_cost + 1; + if (total_cost > 1000) then + cost_limit_exceed; + res := 0; + for i := 1 to length(secret_word) do + if secret_word[i] = c then + inc(res); + exit(res); +end; + +function get_char_at_pos(x: longint): char; +begin + total_cost := total_cost + 10; + if (total_cost > 1000) then + cost_limit_exceed; + if (x < 1) or (x > length(secret_word)) then + exit('#'); + exit(secret_word[x]); +end; + +procedure check_secret_word; +var + f: text; + i: longint; + ok: boolean; +begin + assign(f, 'DIC.DAT'); + reset(f); + while not seekeof(f) do + begin + inc(n); + readln(f, words[n]); + end; + close(f); + ok := false; + for i := 1 to n do + if words[i] = secret_word then ok := true; + if not ok then + begin + writeln('Du lieu duoc khoi tao khong chinh xac. Dap an can tim khong nam trong tu dien'); + halt; + end; +end; + +procedure init; +begin + writeln; + writeln(' TU DIEN '); + writeln('*****************'); + writeln; + + secret_word := 'cat'; + total_cost := 0; + check_secret_word; +end; + +initialization + init; +end. -- cgit 1.4.1