# 1. DS static search sequence

## Title Description

Give a queue and the value to be found, and find the position of the value in the queue. The queue position starts from 1

A sequential search algorithm with sentinels is required

## input

Input n in the first line, indicating that there are n data in the queue
Input n data in the second line, all positive integers, separated by spaces
Enter t in the third line, indicating that there are t values to find
From the fourth line, enter t values and enter t lines

## output

Each line outputs a value to be searched in the queue. If the search is unsuccessful, the string error is output

## sample input

8
33 66 22 88 11 27 44 55
3
22
11
99

3
5
error

## Reference code

```#include<iostream>
using namespace std;
void search(int e, int array[], int t)
{
for (int i = t - 1;i >= 0;i--)
{
if (array[i] == e)
{
if (i == 0)cout << "error\n";
else
{
cout << i << endl;
break;
}
}
}
}
int main()
{
int* array;
int t, n;
cin >> t;
array = new int[t+1];
for (int i = 1;i <= t;i++)
cin >> array[i];
cin >> n;
int e;
while (n--)
{
cin >> e;
array = e;
search(e, array,t);
}
return 0;
}
```

# 2. Half search of DS static search

## Title Description

Give a queue and the value to be found, and find the position of the value in the queue. The queue position starts from 1

A half search algorithm is required

## input

Input n in the first line, indicating that there are n data in the queue
Input n data in the second line, all positive integers, separated by spaces
Enter t in the third line, indicating that there are t values to find
From the fourth line, enter t values and enter t lines

## output

Each line outputs a value to be searched in the queue. If the search is unsuccessful, the string error is output

## sample input

8
11 22 33 44 55 66 77 88
3
22
88
99

2
8
error

## Reference code

```#include<iostream>
using namespace std;

void search(int e, int array[], int t)
{
int low = 1, height = t;
int mid;
mid = (low + height) / 2;
for (;low <= height;)
{
if (array[mid] == e)
{
cout << mid << endl;
break;
}
else if (e > array[mid]) { low = mid + 1;mid = (low + height) / 2; }
else if (e < array[mid]) { height = mid - 1;mid = (low + height) / 2; }
}
if (low > height)cout << "error\n";
}
int main()
{
int t, n;
int* array;
cin >> t;
array = new int[t+1];
for (int i = 1;i <= t;i++)
cin >> array[i];
cin >> n;
int e;
while (n--)
{
cin >> e;
search(e, array, t);
}
return 0;
}
```

# 3. Sequential index search of DS static search

## Title Description

Give a queue and the value to be found, and find the position of the value in the queue. The queue position starts from 1

It is required to use the sequential index search algorithm, in which the index table search and in block search adopt the sequential search method of starting from scratch without sentry.

## input

Enter n in the first row, indicating that there are n data in the main table
Input n data in the second line, all positive integers, separated by spaces
Enter K in the third line, indicating that the main table is divided into k blocks, and K is also the length of the index table
In the fourth row, enter k data to represent the maximum value of each block in the index table
Enter t in the fifth line, indicating that there are t values to find
From the sixth line, enter t values and enter t lines

## output

Each line outputs a numerical value to be searched. The position of the queue and the number of searches are separated by dashes. If the search is unsuccessful, the string error is output

## sample input

18
22 12 13 8 9 20 33 42 44 38 24 48 60 58 74 57 86 53
3
22 48 86
6
13
5
48
40
53
90

3-4
error
12-8
error
18-9
error

## Reference code

```#include<iostream>
using namespace std;

int main()
{
int t;
int* array;
cin >> t;
array = new int[t + 1];
for (int i = 1;i <= t;i++)
cin >> array[i];
int k;
cin >> k;
int* maxnum = new int[k];
for (int i = 0;i < k;i++)
cin >> maxnum[i];
int *index=new int[k - 1];
index = 1;
for (int i = 1, j = 0;i <= t;i++)
{
if (array[i] > maxnum[j])
{
index[j+1] = i;
j++;
}

}
int n,e;
cin >> n;
int count;
while (n--)
{
count = 0;
cin >> e;
if (e <= maxnum[k - 1])
{
int i, j;
for (j = 0;j < k;j++)
{
count++;
if (e <= maxnum[j])
{
i = index[j];
break;
}
}
int temp;
if (j == k - 1)temp = t;
else temp = index[j + 1];
for (;i <= temp;i++)
{
count++;
if (e == array[i])
{
cout << i << "-" << count << endl;
break;
}
}
if (i > temp)cout << "error\n";
}
else cout << "error\n";
}
return 0;
}
```

# 4. DS search - find the square root by half search

## Title Description

Assuming that the input y is an integer, we use a half search to find the square root. There must be a value between 0 and y that is the square root of Y. if the number x we are looking for is smaller than the square root of Y, then x2 < y. if the number x we are looking for is larger than the square root of Y, then x2 > y, we can narrow the search range accordingly. When the number we are looking for is accurate enough (for example, if | x2-y | < 0.00001), we can think that the square root of Y has been found.

For example, if you find the square root X of 5, X must satisfy 0 < = x < = 5, and take x as (5 + 0) / 2 = 2.5. Because the square of 2.5 is 6.25 > 5, X must be less than 2.5, that is, X satisfies 0 < = x < = 2.5, and take x as 1.25, and so on

Left endRight endValue of x x 2 x^2 x2 x 2 − y x^2-y x2−y
052.56.251.25
02.51.251.5625-3.4375
1.252.51.8753.515625-1.484375
1.8752.52.18754.78515625-0.21484375
2.18752.52.343755.49316406250.4931640625
2.18752.343752.2656255.1330566406250.133056640625
2.18752.2656252.2265625......

Finally, the square root of 5 is 2.236

Warm tip: in order to ensure accuracy during calculation, double is used as the type of calculation variables

Keep decimal places. Please output in printf("%.3lf\n",x)

The program framework refers to the method of half search in usual practice

## input

In line 1, enter an integer n (< 100), indicating that there are n numbers

Enter n integers from line 2 to line n+1

## output

Output the square root of n numbers, accurate to three decimal places.

2
13
5

3.606
2.236

## Reference code

```#include<iostream>
#include<cmath>
#include<iomanip>
using namespace std;

void getsqrt(double number)
{
double n2=number;
double n1 = 0;
double n;
while (1)
{
n = (n1 + n2) / 2;
if (abs(n * n - number) < 0.00001)
{
cout << fixed << setprecision(3) << n << endl;
break;
}
if (n * n > number)n2 = n;
else n1 = n;
}
}
int main()
{
int n;
cin >> n;
double number;
while (n--)
{
cin >> number;
getsqrt(number);
}
return 0;
}
```

Posted on Sun, 19 Sep 2021 19:47:22 -0400 by hofmann777