#include<stdio.h>
#include<stdlib.h>
int idxsearch(int ar[], int first, int last, int target) {
int mid = (first + last) / 2;
if (first > last)
return -1;
else {
if (ar[mid] == target)
return mid;
else {
if (ar[mid] > target) {
last = mid - 1;
idxsearch(ar, first, last, target);
}
else {
first = mid + 1;
idxsearch(ar, first, last, target);
}
}
}
}
int main() {
scanf_s(" %d", &num);
int *inorder = malloc(sizeof(int)*num);
int *levelorder = malloc(sizeof(int)*num);
for (int i = 0; i < num; i++)
scanf_s(" %d", &inorder[i],sizeof(int));
for (int j = 0; j < num; j++)
scanf_s(" %d", &levelorder[j],sizeof(int));
printf("\n");
printf(" %d\n", idxsearch(inorder, 0, num-1, 3));
printf(" %d\n", idxsearch(levelorder, 0, num-1, 1));
//printsol();
free(levelorder);
free(inorder);
}
The first column shows how many data to enter In the second column, enter as many data as you entered.
Intentions A program that outputs where the index is based on the entered data
Input data 3 3 2 1 3 2 1
If I do this, the output 0 2 It's not normal until
I can't find anything -1 -1 I'm going to print it out, but I don't know.
The environment is windows 10, and I wrote visual studio 2017.
I'd really appreciate it if you could answer me.
c dynamic-allocation struct array
A binary search algorithm is an algorithm for locating specific values in a list sorted in ascending order. - Wikiback
Put the input values into ascending integers~
© 2024 OneMinuteCode. All rights reserved.