알고리즘

[알고리즘] 선형검색 문제

FORHAPPy 2021. 10. 1. 14:09
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
using namespace std;
 
int Search(int* Arr, const int n)
{    
    Arr[n-1= 3;
    int i = 0;
    while(true)
    {
        if (Arr[i] == 3)
            break;
        i++;
    }
    return n-1 == i ? -1 : i;
 
}
 
int main()
{
    int iArr[8= { 6432198, };
    const int nx = 8;
    int idx = 0;
    
    idx = Search(iArr, nx);
 
    cout << '\t' << "|";
    for (int i = 0; i < 7++i)
    {
        cout << i << '\t';
    }
    cout << endl;
    cout << "--------+-----------------------------------------------------" << endl;
    for (int k = 0; k < idx + 1++k)
    {
        cout << '\t' << "|";
        for (int j = 0; j < 6++j)
        {
            if (k == j)
            {
                cout << "*" << '\t';
            }
            else
            {
                cout << '\t';
            }
        }
        cout << endl;
        cout << '\t' << "|";
        for (int l = 0; l < 7++l)
        {
            cout << iArr[l] << '\t';
        }
        cout << endl;
    }
    cout << 3 << "은" << "iArr[" << idx << "]" << "에 존재 합니다." << endl;
}
cs

 

 

<결과>

 

'알고리즘' 카테고리의 다른 글

[알고리즘] 버블정렬, 단순 선택 정렬, 단순 삽입 정렬  (0) 2021.10.04
[알고리즘] 스택  (0) 2021.10.01
[알고리즘] 이진검색  (0) 2021.10.01
[알고리즘] 선형검색  (0) 2021.10.01
[알고리즘] 구조체  (0) 2021.09.30