/*
* 找出10个数中的值
*/
#include <iostream>
using namespace std;
int ar[10];
int n;
void in();
void out();
int solve();
void sort();
int main()
{
cout << "请输入10个整数:" << endl;
in();
sort();
out();
cout << "请输入要查找的值:" << endl;
cin >> n;
if (solve() != -1)
{
cout << "有!" << endl;
}
else
{
cout << "没有该值!" << endl;
}
return 0;
}
void in()
{
int i = 0;
for (i = 0; i < 10; ++i)
{
cin >> ar[i];
}
}
void out()
{
int i = 0;
for (i = 0; i < 10; ++i)
{
cout << ar[i] << " ";
}
cout << endl;
}
int solve()
{
int end = 10;
int start = 0;
int mid = (end + start) / 2;
while (end - start >= 1)
{
mid = (end + start) / 2;
if (ar[mid] == n)
{
return 1;
}
else if (ar[mid] > n)
{
end = mid;
}
else
{
start = mid + 1;
}
}
return -1;
}
void sort()
{
int i;
int j;
for (i = 0; i < 9; ++i)
{
for (j = i+1; j < 10; ++j)
{
if (ar[j] < ar[i])
{
int temp = ar[j];
ar[j] = ar[i];
ar[i] = temp;
}
}
}
}