#include<stdio.h>
int bin_search(int arr[], int left, int right, int key)
{
int mid = 0;
while (left <= right)
{
mid = left + (right - left) / 2;
if (arr[mid] < key)
{
left = mid + 1;
}
else if (arr[mid] == key)
{
return arr[mid];
}
else
{
right = mid - 1;
}
}
return -1;
}
int main()
{
int arr[10] = {0,1,2,3,4,5,6,7,8,9};
int len = sizeof(arr) / sizeof(arr[0]);
int ret = bin_search(arr, 0, len - 1, 6);
printf("%d", ret);
}
空瓶子问题:有二十块钱,一块钱一瓶汽水,两个空瓶子能换一瓶汽水,一共能喝多少瓶汽水
#include<stdio.h>
int bottle(int total, int empty)
{
while (empty > 1)
{
total = total + empty / 2;
empty = empty / 2 + empty % 2;
}
return total;
}
int main()
{
int ret=bottle(20,20);
printf("%d", ret);
return 0;
}
打印菱形
#include<stdio.h>
void Func(int line)
{//菱形的上半部分
for (int i = 0; i < line; i++)
{
for (int j =0; j < line-1-i; j++)//打印空格
{
printf(" ");
}
for (int j = 0; j < 2 * i + 1; j++)
{
printf("*");
}
printf("\n");
}
//下半部分
for (int i = 0; i < line - 1; i++)
{
for (int j = 0; j < i + 1; j++)
{
printf(" ");
}
for (int j = 0; j < 2 * (line - 1-i)-1; j++)
{
printf("*");
}
printf("\n");
}
}
int main()
{
Func(13);
return 0;
}