diff options
author | Raphael McSinyx <vn.mcsinyx@gmail.com> | 2017-02-15 11:33:54 +0700 |
---|---|---|
committer | Raphael McSinyx <vn.mcsinyx@gmail.com> | 2017-02-15 11:33:54 +0700 |
commit | 5a44eb260b9b35096df7371a702ba99b7265b3f9 (patch) | |
tree | 60ad135076dbca49d1e20eac540c53bdd6e8b97a | |
parent | fa7cbaee4baf174ee416280d82a60fade6d3eaad (diff) | |
download | cp-5a44eb260b9b35096df7371a702ba99b7265b3f9.tar.gz |
Update others/volume1
33 files changed, 432 insertions, 116 deletions
diff --git a/others/volume1/002.pas b/others/volume1/002.pas index a60a15c..7cbdd50 100644 --- a/others/volume1/002.pas +++ b/others/volume1/002.pas @@ -1,4 +1,4 @@ -uses cmath; +uses clib; var m: int64; diff --git a/others/volume1/004.pas b/others/volume1/004.pas index 9ade431..e619f50 100644 --- a/others/volume1/004.pas +++ b/others/volume1/004.pas @@ -1,4 +1,4 @@ -uses cmath; +uses clib; var prime: array [2..1000000] of boolean; diff --git a/others/volume1/006.pas b/others/volume1/006.pas index 878f077..2413c32 100644 --- a/others/volume1/006.pas +++ b/others/volume1/006.pas @@ -1,4 +1,4 @@ -uses cmath; +uses clib; var a, b, g: int64; diff --git a/others/volume1/013.pas b/others/volume1/013.pas index d257945..98f5df9 100644 --- a/others/volume1/013.pas +++ b/others/volume1/013.pas @@ -1,4 +1,4 @@ -uses cmath; +uses clib; var n: shortint; diff --git a/others/volume1/014.pas b/others/volume1/014.pas index 3407e70..fa98e0a 100644 --- a/others/volume1/014.pas +++ b/others/volume1/014.pas @@ -1,4 +1,4 @@ -uses cmath; +uses clib; var n: shortint; diff --git a/others/volume1/018.pas b/others/volume1/018.pas index 053095c..0e2d293 100644 --- a/others/volume1/018.pas +++ b/others/volume1/018.pas @@ -1,4 +1,4 @@ -uses cmath; +uses clib; var n: int64; diff --git a/others/volume1/023.pas b/others/volume1/023.pas index 6092889..80feb29 100644 --- a/others/volume1/023.pas +++ b/others/volume1/023.pas @@ -1,4 +1,4 @@ -uses strutils, cmath; +uses strutils, clib; var n, u: int64; diff --git a/others/volume1/024.pas b/others/volume1/024.pas index affcbaa..df07a88 100644 --- a/others/volume1/024.pas +++ b/others/volume1/024.pas @@ -1,4 +1,4 @@ -uses cmath; +uses clib; var n: int64; diff --git a/others/volume1/027.pas b/others/volume1/027.pas index 0b285bd..5f57027 100644 --- a/others/volume1/027.pas +++ b/others/volume1/027.pas @@ -1,4 +1,4 @@ -uses cmath; +uses clib; var n: int64; diff --git a/others/volume1/029.pas b/others/volume1/029.pas index 255331f..c590f47 100644 --- a/others/volume1/029.pas +++ b/others/volume1/029.pas @@ -1,4 +1,4 @@ -uses sortnfind; +uses clib; var n, i: int16; diff --git a/others/volume1/030.pas b/others/volume1/030.pas index 95490f2..ffa983b 100644 --- a/others/volume1/030.pas +++ b/others/volume1/030.pas @@ -1,17 +1,8 @@ +uses clib; + var s: ansistring; -function ispalindrome(s: ansistring): boolean; - var - n, i: int32; - begin - n := length(s); - for i := 1 to n do - if s[i] <> s[n - i + 1] then - exit(false); - ispalindrome := true - end; - begin readln(s); if ispalindrome(s) then diff --git a/others/volume1/032.pas b/others/volume1/032.pas index 7813997..f9c823a 100644 --- a/others/volume1/032.pas +++ b/others/volume1/032.pas @@ -1,4 +1,4 @@ -uses sortnfind; +uses clib; var n, i: int16; diff --git a/others/volume1/034.pas b/others/volume1/034.pas index 1e34793..2b21194 100644 --- a/others/volume1/034.pas +++ b/others/volume1/034.pas @@ -1,4 +1,4 @@ -uses math, sortnfind; +uses math, clib; var n, i: int16; diff --git a/others/volume1/035.pas b/others/volume1/035.pas index ccf9560..649e398 100644 --- a/others/volume1/035.pas +++ b/others/volume1/035.pas @@ -1,17 +1,9 @@ +uses clib; + var n, a: int16; b: array of boolean; -function all(b: array of boolean): boolean; - var - i: int16; - begin - for i := 0 to length(b) - 1 do - if not b[i] then - exit(false); - all := true - end; - begin readln(n); setlength(b, n); diff --git a/others/volume1/045.pas b/others/volume1/045.pas index eca1c9d..b1917ee 100644 --- a/others/volume1/045.pas +++ b/others/volume1/045.pas @@ -1,4 +1,4 @@ -uses cmath; +uses clib; var n: int8 = 0; diff --git a/others/volume1/050.pas b/others/volume1/050.pas new file mode 100644 index 0000000..cae9eb4 --- /dev/null +++ b/others/volume1/050.pas @@ -0,0 +1,9 @@ +uses math, strutils; + +var + n: int64; + +begin + readln(n); + writeln(inttobin(n, trunc(log2(n)) + 1)) +end. diff --git a/others/volume1/051.pas b/others/volume1/051.pas new file mode 100644 index 0000000..d19fe39 --- /dev/null +++ b/others/volume1/051.pas @@ -0,0 +1,16 @@ +var + n, i, v, a: int32; + +begin + readln(n, v); + for i := 1 to n do + begin + read(a); + if a = v then + begin + writeln('YES'#10, i); + exit + end; + end; + writeln('NO') +end. diff --git a/others/volume1/052.pas b/others/volume1/052.pas new file mode 100644 index 0000000..8a071a0 --- /dev/null +++ b/others/volume1/052.pas @@ -0,0 +1,24 @@ +uses clib; + +var + n, a: int16; + b: array of boolean; + +begin + readln(n); + setlength(b, n); + for n := n - 1 downto 0 do + b[n] := false; + for n := 1 to length(b) do + begin + read(a); + if (a <= 0) or + (a > length(b)) then + continue; + b[a - 1] := true + end; + if all(b) then + writeln('YES') + else + writeln('NO') +end. diff --git a/others/volume1/053.pas b/others/volume1/053.pas new file mode 100644 index 0000000..f3c8a4e --- /dev/null +++ b/others/volume1/053.pas @@ -0,0 +1,20 @@ +var + n, i: int16; + a: array of int32; + v: int32; + +begin + readln(n); + setlength(a, n + 1); + for i := 0 to n - 1 do + read(a[i]); + readln(i, v); + repeat + a[n] := a[n - 1]; + dec(n) + until n = i; + a[i - 1] := v; + for i := 0 to length(a) - 2 do + write(a[i], ' '); + writeln(a[length(a) - 1]) +end. diff --git a/others/volume1/054.pas b/others/volume1/054.pas new file mode 100644 index 0000000..ad29f4d --- /dev/null +++ b/others/volume1/054.pas @@ -0,0 +1,21 @@ +uses clib; + +var + n, c: int16; + a: int32; + s: int64 = 0; + +begin + readln(n); + c := 0; + repeat + read(a); + if issquare(a) then + begin + inc(c); + s := s + a + end; + dec(n) + until n = 0; + writeln(c, ' ', s) +end. diff --git a/others/volume1/055.pas b/others/volume1/055.pas new file mode 100644 index 0000000..0e57447 --- /dev/null +++ b/others/volume1/055.pas @@ -0,0 +1,18 @@ +var + n, i, j: int16; + a, min: int64; + +begin + min := 9223372036854775807; + readln(n); + for i := 1 to n do + begin + read(a); + if a < min then + begin + j := i; + min := a + end + end; + writeln(j, ' ', min) +end. diff --git a/others/volume1/056.pas b/others/volume1/056.pas new file mode 100644 index 0000000..8fdc2dc --- /dev/null +++ b/others/volume1/056.pas @@ -0,0 +1,17 @@ +uses sysutils, clib; + +var + n, count: int16; + a: int64; + +begin + count := 0; + readln(n); + repeat + readln(a); + if ispalindrome(inttostr(a)) then + inc(count); + dec(n) + until n = 0; + writeln(count) +end. diff --git a/others/volume1/057.pas b/others/volume1/057.pas new file mode 100644 index 0000000..91fc22d --- /dev/null +++ b/others/volume1/057.pas @@ -0,0 +1,14 @@ +var + n, i, j: int16; + a: int64; + +begin + readln(n, i); + for j := 1 to n do + begin + read(a); + if i <> j then + write(a, ' ') + end; + writeln +end. diff --git a/others/volume1/058.pas b/others/volume1/058.pas new file mode 100644 index 0000000..f5c4d6b --- /dev/null +++ b/others/volume1/058.pas @@ -0,0 +1,16 @@ +uses clib; + +var + n, i: int16; + a: array of int64; + +begin + readln(n); + setlength(a, n); + for i := 0 to n - 1 do + read(a[i]); + qsort(a); + for i := 0 to n - 2 do + write(a[i], ' '); + writeln(a[n - 1]) +end. diff --git a/others/volume1/059.pas b/others/volume1/059.pas new file mode 100644 index 0000000..61095ab --- /dev/null +++ b/others/volume1/059.pas @@ -0,0 +1,17 @@ +uses clib; + +var + n: int16; + a, s: int64; + +begin + s := 0; + readln(n); + repeat + read(a); + if isprime(a) then + s := s + a; + dec(n) + until n = 0; + writeln(s) +end. diff --git a/others/volume1/060.pas b/others/volume1/060.pas new file mode 100644 index 0000000..8fdc2dc --- /dev/null +++ b/others/volume1/060.pas @@ -0,0 +1,17 @@ +uses sysutils, clib; + +var + n, count: int16; + a: int64; + +begin + count := 0; + readln(n); + repeat + readln(a); + if ispalindrome(inttostr(a)) then + inc(count); + dec(n) + until n = 0; + writeln(count) +end. diff --git a/others/volume1/061.pas b/others/volume1/061.pas new file mode 100644 index 0000000..46bf839 --- /dev/null +++ b/others/volume1/061.pas @@ -0,0 +1,21 @@ +var + n, i: int16; + a, b: array of int16; + +begin + readln(n); + setlength(a, n); + setlength(b, n); + for i := 0 to n - 1 do + begin + read(a[i]); + b[a[i] - 1] := i + 1 + end; + for i := 0 to n - 1 do + if a[i] <> b[i] then + begin + writeln('NO'); + exit + end; + writeln('YES') +end. diff --git a/others/volume1/062.pas b/others/volume1/062.pas new file mode 100644 index 0000000..715d005 --- /dev/null +++ b/others/volume1/062.pas @@ -0,0 +1,29 @@ +var + m, n, i: int16; + a: array of int32; + b: int32; + +begin + readln(m, n); + setlength(a, m); + for i := 0 to m - 1 do + read(a[i]); + i := 0; + repeat + read(b); + while (i < m) and + (a[i] <= b) do + begin + write(a[i], ' '); + inc(i) + end; + write(b, ' '); + dec(n) + until n = 0; + while i < m do + begin + write(a[i], ' '); + inc(i) + end; + writeln +end. diff --git a/others/volume1/065.pas b/others/volume1/065.pas new file mode 100644 index 0000000..6e9106c --- /dev/null +++ b/others/volume1/065.pas @@ -0,0 +1,19 @@ +var + n, i, k: int32; + b: array of boolean; + +begin + readln(n, k); + setlength(b, n); + for i := 0 to n - 1 do + b[i] := true; + repeat + read(i); + b[i - 1] := false; + dec(k) + until k = 0; + for i := 0 to n - 1 do + if b[i] then + write(i + 1, ' '); + writeln +end. diff --git a/others/volume1/067.pas b/others/volume1/067.pas new file mode 100644 index 0000000..f5c4d6b --- /dev/null +++ b/others/volume1/067.pas @@ -0,0 +1,16 @@ +uses clib; + +var + n, i: int16; + a: array of int64; + +begin + readln(n); + setlength(a, n); + for i := 0 to n - 1 do + read(a[i]); + qsort(a); + for i := 0 to n - 2 do + write(a[i], ' '); + writeln(a[n - 1]) +end. diff --git a/others/volume1/068.pas b/others/volume1/068.pas new file mode 100644 index 0000000..fa568dc --- /dev/null +++ b/others/volume1/068.pas @@ -0,0 +1,39 @@ +var + n, m, i, j: int16; + a: array of array of int32; + s: int64 = 0; + +begin + readln(n, m); + setlength(a, n + 2); + setlength(a[0], m + 2); + for i := 0 to m + 1 do + a[0][i] := 0; + for i := 1 to n do + begin + setlength(a[i], m + 2); + a[i][0] := 0; + for j := 1 to m do + read(a[i][j]); + a[i][m + 1] := 0 + end; + setlength(a[n + 1], m + 2); + for i := 0 to m + 1 do + a[n + 1][i] := 0; + + for i := 1 to n do + for j := 1 to m do + if a[i][j] <> 0 then + begin + if a[i][j] > a[i - 1][j] then + s := s + a[i][j] - a[i - 1][j]; + if a[i][j] > a[i + 1][j] then + s := s + a[i][j] - a[i + 1][j]; + if a[i][j] > a[i][j - 1] then + s := s + a[i][j] - a[i][j - 1]; + if a[i][j] > a[i][j + 1] then + s := s + a[i][j] - a[i][j + 1]; + s := s + 1 + end; + writeln(s) +end. diff --git a/others/volume1/cmath.pas b/others/volume1/clib.pas index 67b22a5..28dffb2 100644 --- a/others/volume1/cmath.pas +++ b/others/volume1/clib.pas @@ -1,8 +1,11 @@ -(* Common mathematical functions and data *) -unit cmath; +(* Common library *) +unit clib; interface + type + intar = array of int64; + const factorial: array[1..20] of uint64 = ( 1, @@ -122,12 +125,57 @@ interface 12200160415121876738 ); + procedure qsort(var a : intar); function gcd(x, y: int64): int64; function isprime(x: int64): boolean; function issquare(x: int64): boolean; + function ispalindrome(s: ansistring): boolean; + function all(b: array of boolean): boolean; + function binin( + a: intar; + x: int64 + ): boolean; + implementation + procedure qsort(var a : intar); + procedure sort(l, r: int64); + var + i, j, x, y: int64; + + begin + i := l; + j := r; + x := a[(l + r) div 2]; + + repeat + while a[i] < x do + i := i + 1; + while x < a[j] do + j := j - 1; + + if i <= j then + begin + y := a[i]; + a[i] := a[j]; + a[j] := y; + i := i + 1; + j := j - 1 + end + until i > j; + + if l < j then + sort(l, j); + if i < r then + sort(i, r) + end; + + begin + sort(0, length(a) - 1) + end; + + function gcd(x, y: int64): int64; var z: int64; begin @@ -140,6 +188,7 @@ implementation gcd := x end; + function isprime(x: int64): boolean; var i: int32; begin @@ -151,9 +200,60 @@ implementation isprime := true end; + function issquare(x: int64): boolean; begin issquare := (x >= 0) and (sqr(trunc(sqrt(x))) = x) end; + + function ispalindrome(s: ansistring): boolean; + var + n, i: int32; + begin + n := length(s); + for i := 1 to n div 2 do + if s[i] <> s[n - i + 1] then + exit(false); + ispalindrome := true + end; + + + function all(b: array of boolean): boolean; + var + i: boolean; + begin + for i in b do + if not i then + exit(false); + all := true + end; + + + function binin( + a: intar; + x: int64 + ): boolean; + + var + l, h, mid: uint64; + + begin + l := 0; + h := length(a) - 1; + + while l <= h do + begin + mid := (l + h) div 2; + if x = a[mid] then + exit(true) + else if x < a[mid] then + h := mid - 1 + else + l := mid + 1 + end; + + binin := false + end; + end. diff --git a/others/volume1/sortnfind.pas b/others/volume1/sortnfind.pas deleted file mode 100644 index 24f48f6..0000000 --- a/others/volume1/sortnfind.pas +++ /dev/null @@ -1,80 +0,0 @@ -unit sortnfind; - -interface - - type - intar = array of int64; - - procedure qsort(var a : intar); - - function binin( - a: intar; - x: int64 - ): boolean; - -implementation - - procedure qsort(var a : intar); - procedure sort(l, r: int64); - var - i, j, x, y: int64; - - begin - i := l; - j := r; - x := a[(l + r) div 2]; - - repeat - while a[i] < x do - i := i + 1; - while x < a[j] do - j := j - 1; - - if i <= j then - begin - y := a[i]; - a[i] := a[j]; - a[j] := y; - i := i + 1; - j := j - 1 - end - until i > j; - - if l < j then - sort(l, j); - if i < r then - sort(i, r) - end; - - begin - sort(0, length(a) - 1) - end; - - - function binin( - a: intar; - x: int64 - ): boolean; - - var - l, h, mid: uint64; - - begin - l := 0; - h := length(a) - 1; - - while l <= h do - begin - mid := (l + h) div 2; - if x = a[mid] then - exit(true) - else if x < a[mid] then - h := mid - 1 - else - l := mid + 1 - end; - - binin := false - end; - -end. |