折半查找
int arr[] = { 23, 37, 41, 55, 64, 75, 83, 99, 101 };
int t;
cin >> t;
int low = 0, high = sizeof(arr) / sizeof(arr[0]);
int mid = 0;
while (low <= high)
{
mid = (low + high) / 2;
if (arr[mid] > t)
{
cout << "比t大\n";
high = mid;
}
else if (arr[mid] == t)
{
cout << "找到了, 第 " << mid + 1 << " 个\n";
break;
}
else
{
cout << "比t小\n";
low = mid;
}
}
输出时间
struct CLOCK
{
int hour;
int minute;
int second;
};
int main()
{
CLOCK clock{ 0,0,0 };
int n = 100, i = 0;
while (++i < n)
{
clock.second++;
if (clock.second > 59)
{
clock.second -= 60;
clock.minute++;
}
if (clock.minute > 59)
{
clock.minute -= 60;
clock.hour++;
}
if (clock.hour > 23)
{
clock.second -= 24;
}
cout.fill('0');
cout << setw(2) << clock.hour << " : ";
cout.fill('0');
cout << setw(2) << clock.minute << " : ";
cout.fill('0');
cout << setw(2) << clock.second << endl;
Sleep(1000);
}
return 0;
}