From 5a44eb260b9b35096df7371a702ba99b7265b3f9 Mon Sep 17 00:00:00 2001 From: Raphael McSinyx Date: Wed, 15 Feb 2017 11:33:54 +0700 Subject: Update others/volume1 --- others/volume1/002.pas | 2 +- others/volume1/004.pas | 2 +- others/volume1/006.pas | 2 +- others/volume1/013.pas | 2 +- others/volume1/014.pas | 2 +- others/volume1/018.pas | 2 +- others/volume1/023.pas | 2 +- others/volume1/024.pas | 2 +- others/volume1/027.pas | 2 +- others/volume1/029.pas | 2 +- others/volume1/030.pas | 13 +-- others/volume1/032.pas | 2 +- others/volume1/034.pas | 2 +- others/volume1/035.pas | 12 +- others/volume1/045.pas | 2 +- others/volume1/050.pas | 9 ++ others/volume1/051.pas | 16 +++ others/volume1/052.pas | 24 ++++ others/volume1/053.pas | 20 ++++ others/volume1/054.pas | 21 ++++ others/volume1/055.pas | 18 +++ others/volume1/056.pas | 17 +++ others/volume1/057.pas | 14 +++ others/volume1/058.pas | 16 +++ others/volume1/059.pas | 17 +++ others/volume1/060.pas | 17 +++ others/volume1/061.pas | 21 ++++ others/volume1/062.pas | 29 +++++ others/volume1/065.pas | 19 ++++ others/volume1/067.pas | 16 +++ others/volume1/068.pas | 39 +++++++ others/volume1/clib.pas | 259 +++++++++++++++++++++++++++++++++++++++++++ others/volume1/cmath.pas | 159 -------------------------- others/volume1/sortnfind.pas | 80 ------------- 34 files changed, 589 insertions(+), 273 deletions(-) create mode 100644 others/volume1/050.pas create mode 100644 others/volume1/051.pas create mode 100644 others/volume1/052.pas create mode 100644 others/volume1/053.pas create mode 100644 others/volume1/054.pas create mode 100644 others/volume1/055.pas create mode 100644 others/volume1/056.pas create mode 100644 others/volume1/057.pas create mode 100644 others/volume1/058.pas create mode 100644 others/volume1/059.pas create mode 100644 others/volume1/060.pas create mode 100644 others/volume1/061.pas create mode 100644 others/volume1/062.pas create mode 100644 others/volume1/065.pas create mode 100644 others/volume1/067.pas create mode 100644 others/volume1/068.pas create mode 100644 others/volume1/clib.pas delete mode 100644 others/volume1/cmath.pas delete mode 100644 others/volume1/sortnfind.pas (limited to 'others/volume1') 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/clib.pas b/others/volume1/clib.pas new file mode 100644 index 0000000..28dffb2 --- /dev/null +++ b/others/volume1/clib.pas @@ -0,0 +1,259 @@ +(* Common library *) +unit clib; + +interface + + type + intar = array of int64; + + const + factorial: array[1..20] of uint64 = ( + 1, + 2, + 6, + 24, + 120, + 720, + 5040, + 40320, + 362880, + 3628800, + 39916800, + 479001600, + 6227020800, + 87178291200, + 1307674368000, + 20922789888000, + 355687428096000, + 6402373705728000, + 121645100408832000, + 2432902008176640000 + ); + fibonacci: array[1..93] of uint64 = ( + 1, + 1, + 2, + 3, + 5, + 8, + 13, + 21, + 34, + 55, + 89, + 144, + 233, + 377, + 610, + 987, + 1597, + 2584, + 4181, + 6765, + 10946, + 17711, + 28657, + 46368, + 75025, + 121393, + 196418, + 317811, + 514229, + 832040, + 1346269, + 2178309, + 3524578, + 5702887, + 9227465, + 14930352, + 24157817, + 39088169, + 63245986, + 102334155, + 165580141, + 267914296, + 433494437, + 701408733, + 1134903170, + 1836311903, + 2971215073, + 4807526976, + 7778742049, + 12586269025, + 20365011074, + 32951280099, + 53316291173, + 86267571272, + 139583862445, + 225851433717, + 365435296162, + 591286729879, + 956722026041, + 1548008755920, + 2504730781961, + 4052739537881, + 6557470319842, + 10610209857723, + 17167680177565, + 27777890035288, + 44945570212853, + 72723460248141, + 117669030460994, + 190392490709135, + 308061521170129, + 498454011879264, + 806515533049393, + 1304969544928657, + 2111485077978050, + 3416454622906707, + 5527939700884757, + 8944394323791464, + 14472334024676221, + 23416728348467685, + 37889062373143906, + 61305790721611591, + 99194853094755497, + 160500643816367088, + 259695496911122585, + 420196140727489673, + 679891637638612258, + 1100087778366101931, + 1779979416004714189, + 2880067194370816120, + 4660046610375530309, + 7540113804746346429, + 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 + while y > 0 do + begin + z := y; + y := x mod y; + x := z + end; + gcd := x + end; + + + function isprime(x: int64): boolean; + var i: int32; + begin + if x < 2 then + exit(false); + for i := 2 to trunc(sqrt(x)) do + if x mod i = 0 then + exit(false); + 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/cmath.pas b/others/volume1/cmath.pas deleted file mode 100644 index 67b22a5..0000000 --- a/others/volume1/cmath.pas +++ /dev/null @@ -1,159 +0,0 @@ -(* Common mathematical functions and data *) -unit cmath; - -interface - - const - factorial: array[1..20] of uint64 = ( - 1, - 2, - 6, - 24, - 120, - 720, - 5040, - 40320, - 362880, - 3628800, - 39916800, - 479001600, - 6227020800, - 87178291200, - 1307674368000, - 20922789888000, - 355687428096000, - 6402373705728000, - 121645100408832000, - 2432902008176640000 - ); - fibonacci: array[1..93] of uint64 = ( - 1, - 1, - 2, - 3, - 5, - 8, - 13, - 21, - 34, - 55, - 89, - 144, - 233, - 377, - 610, - 987, - 1597, - 2584, - 4181, - 6765, - 10946, - 17711, - 28657, - 46368, - 75025, - 121393, - 196418, - 317811, - 514229, - 832040, - 1346269, - 2178309, - 3524578, - 5702887, - 9227465, - 14930352, - 24157817, - 39088169, - 63245986, - 102334155, - 165580141, - 267914296, - 433494437, - 701408733, - 1134903170, - 1836311903, - 2971215073, - 4807526976, - 7778742049, - 12586269025, - 20365011074, - 32951280099, - 53316291173, - 86267571272, - 139583862445, - 225851433717, - 365435296162, - 591286729879, - 956722026041, - 1548008755920, - 2504730781961, - 4052739537881, - 6557470319842, - 10610209857723, - 17167680177565, - 27777890035288, - 44945570212853, - 72723460248141, - 117669030460994, - 190392490709135, - 308061521170129, - 498454011879264, - 806515533049393, - 1304969544928657, - 2111485077978050, - 3416454622906707, - 5527939700884757, - 8944394323791464, - 14472334024676221, - 23416728348467685, - 37889062373143906, - 61305790721611591, - 99194853094755497, - 160500643816367088, - 259695496911122585, - 420196140727489673, - 679891637638612258, - 1100087778366101931, - 1779979416004714189, - 2880067194370816120, - 4660046610375530309, - 7540113804746346429, - 12200160415121876738 - ); - - function gcd(x, y: int64): int64; - function isprime(x: int64): boolean; - function issquare(x: int64): boolean; - -implementation - - function gcd(x, y: int64): int64; - var z: int64; - begin - while y > 0 do - begin - z := y; - y := x mod y; - x := z - end; - gcd := x - end; - - function isprime(x: int64): boolean; - var i: int32; - begin - if x < 2 then - exit(false); - for i := 2 to trunc(sqrt(x)) do - if x mod i = 0 then - exit(false); - isprime := true - end; - - function issquare(x: int64): boolean; - begin - issquare := (x >= 0) and (sqr(trunc(sqrt(x))) = x) - 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. -- cgit 1.4.1