Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include "stdio.h"
- #include <iostream>
- int main() {
- setlocale(0, "Russian");
- const int N = 8;
- short A[N] = {-9, -8, -6, 0, 4, 5, 6, 10}, lN = 0, rN = N - 1, len, standart, n;
- bool goOut = false;
- //for (int i = 0; i < N; i++) {
- // printf("A[%d] = ", i);
- // scanf("%hd", &A[i]);
- //}
- //puts("");
- for (int i = 0; i < N; i++) {
- for (int j = 0; j < N - 1; j++) {
- if (A[j] > A[j + 1]) {
- short t = A[j];
- A[j] = A[j + 1];
- A[j + 1] = t;
- }
- }
- }
- printf("Введите элемент, который хотите найти: ");
- scanf("%hd", &standart);
- while (!goOut) {
- len = rN - lN;
- n = len / 2 + lN;
- if (standart < A[n]) {
- rN = n - 1;
- }
- if (standart > A[n]) {
- lN = n + 1;
- }
- if (standart == A[n]) {
- printf("Элемент найден на %d позиции\n", n);
- goOut = true;
- }
- if (standart != A[n] && len < 1) {
- printf("Такого элемента нет\n");
- goOut = true;
- }
- }
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement