#include<iostream>
#include<stdlib.h>
using namespace std;
#define MaxNumber 100
int *Number = new int[MaxNumber];
void found(int a, int data[],int b, int length) {
if (b + 1 == length) {
if (data[b] == a) {
cout << "--------------------------" << endl;
cout << "已找到,在第" << b+1 << "位" << endl;
cout << "--------------------------" << endl;
}
else {
if (data[length] == a) {
cout << "--------------------------" << endl;
cout << "已找到,在第" << (length + b) / 2 + 1 << "位" << endl;
cout << "--------------------------" << endl;
}
else {
cout << "--------------------------" << endl;
cout << "陣列裡沒有這個數字" << endl;
cout << "--------------------------" << endl;
}
}
}
else {
if (data[(length+b) / 2] > a) {
found(a, data, 0, (length + b) / 2);
}
if (data[(length+b) / 2] < a) {
found(a, data, (length + b) / 2, length);
}
if (data[(length + b) / 2] == a) {
cout << "--------------------------" << endl;
cout << "已找到,在第" << (length + b) / 2+1 << "位" << endl;
cout << "--------------------------" << endl;
}
}
}
int main() {
int length = 0;
cout << "輸入陣列長度:";
cin >> length;
cout << endl;
cout << "輸入陣列:";
for (int i = 0; i < length; i++)
{
cin >> Number[i];
}
cout << endl;
cout << "-----------------------------" << endl;
cout << " 資料錄入完畢 " << endl;
cout << "-----------------------------" << endl;
int num = 0;
for (int i = 0; i < length; i++)
{
for (int j = i+1; j < length; j++)
{
if (Number[i] > Number[j]) {
num = Number[i];
Number[i] = Number[j];
Number[j] = num;
}
}
}
cout << "陣列為:";
for (int i = 0; i < length; i++)
{
cout<<Number[i]<<" ";
}
cout << endl;
bool a = true;
while (a) {
int number = 0;
cout << "輸入需要搜尋的數字:";
cin >> number;
found(number,Number,0,length-1);
cout << "是否繼續?(是輸入1,否輸入其他數)" << endl;
int b = 0;
cin >> b;
if (b == 1) {
a = true;
}
else {
a = false;
}
}
}