aboutsummaryrefslogtreecommitdiff
path: root/Semestr 4/ask/lista5/zad4.c
diff options
context:
space:
mode:
Diffstat (limited to 'Semestr 4/ask/lista5/zad4.c')
-rw-r--r--Semestr 4/ask/lista5/zad4.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/Semestr 4/ask/lista5/zad4.c b/Semestr 4/ask/lista5/zad4.c
new file mode 100644
index 0000000..a6927fb
--- /dev/null
+++ b/Semestr 4/ask/lista5/zad4.c
@@ -0,0 +1,66 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+
+int puzzle4(long *a, long v, uint64_t s, uint64_t e);
+
+long tab[100];
+
+int assembly_like(long *rdi, long rsi, uint64_t rdx, uint64_t rcx) {
+ uint64_t rax = rcx;
+ rax -= rdx;
+ rax >>= 1;
+ rax += rdx;
+ if (rcx < rdx) {
+ goto L5;
+ }
+ long r8 = *(rdi + rax*8);
+ if (rsi == r8) {
+ goto L10;
+ }
+ if (rsi > r8) {
+ goto L11;
+ }
+ rdx = rax + 1;
+ assembly_like(rdi, rsi, rdx, rcx);
+L10:
+ return (int)rax;
+L11:
+ rcx = rax - 1;
+ assembly_like(rdi, rsi, rdx, rcx);
+ return (int)rax;
+L5:
+ return -1;
+}
+
+
+// binsearch :)
+int decode(long *a, long v, uint64_t s, uint64_t e) {
+ int result = (e + s) / 2;
+ if (e < s) {
+ return -1;
+ }
+ long val = a[result];
+ if (v == val) {
+ return result;
+ }
+ if (v < val) {
+ e = result - 1;
+ }
+ else {
+ s = result + 1;
+ }
+ return decode(a, v, s, e);
+}
+
+int main() {
+ uint64_t n;
+ long v;
+ scanf("%ld %ld", &n, &v);
+ for (int i = 0; i < n; i++) {
+ scanf("%ld", &tab[i]);
+ }
+ printf("%d\n", puzzle4(tab, v, 0, n-1));
+ printf("%d\n", decode(tab, v, 0, n-1));
+
+} \ No newline at end of file