-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path10474.CPP
executable file
·59 lines (52 loc) · 1.25 KB
/
10474.CPP
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
57
58
59
/* Problem: Where is the Marble? UVa 10474
Programmer: Md. Mahmud Ahsan
Compiled: Visual C++ 6.0
Date: 14-05-04
*/
#include <iostream.h>
#include <stdlib.h>
//using namespace std;
// use for built in quick sort
int cmp_func(const void *a, const void * b){
int *_a, *_b;
_a = (int *) a;
_b = (int *) b;
if ( *_a < *_b) return -1;
else if (*_a == *_b) return 0;
else return 1;
}
int linearSearch(int array[], int n, int item);
//===============================================
int main(){
int *data;
int n, q, result, query;
int i, cases = 0;
while (cin >> n >> q){
if (n == 0 && q == 0) break;
data = new int[n];
for (i = 0; i < n; ++i)
cin >> data[i];
qsort(data, n, sizeof(data), cmp_func);
cout << "CASE# " << ++cases << ":" << endl;
for (i = 0; i < q; ++i){
cin >> query;
result = linearSearch(data, n, query);
if (result != -1){
cout << query << " found at " << ++result << endl;
}
else
cout << query << " not found" << endl;
}
delete[] data;
}
return 0;
}
//================================================
int linearSearch(int array[], int n, int item){
int i;
for (i = 0; i < n; ++i){
if (array[i] == item)
return i;
}
return -1;
}