在一个有序数组中查找具体的某个数字n。(讲解二分查找)
#define _CRT_SECURE_NO_WARNINGS 1
//添加这个是为了能在多个编译器兼容(鹏哥视频里有提到)
#include <stdio.h>
#include <string.h>
int main()
{
int arr1[] = { 0,1,2,3,4,5,6,7,8,9};
int k = 8;
int sz= 0;
int sz = sizeof(arr1)/sizeof(arr1[0]);
/*int sz = strlen (arr1)+1;*/
int i = 0;
for (i = 0; i < sz; i++)
{
if (k == arr1[i])
{
printf("找到啦:i=%d", i);
break;
}
}
if (i == sz)
printf("没有找到你想要的数字k\n");
return 0;
}
编写代码,演示多个字符从两端移动,向中间汇聚
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include<windows.h>
#include<stdlib.h>
int main()
{
char arr1[] = "welcome to bite";
char arr2[] = "###############";
int left = 0;
int right = strlen(arr1)-1;
//strlen这个关键字代表sizeof(arr1)/sizeof(arr1[0])-1
printf("%s\n", arr2);
while (left <= right)
{
Sleep(1000);//ms,延时,便于观察
system("cls");//清空屏幕
arr2[left] = arr1[left];
arr2[right] = arr1[right];
left++;
right--;
printf("%s\n", arr2);
}
return 0;
}
今天学到了新的几个关键字
system("cls");//清空屏幕,头文件在<stdlib.h>
Sleep(1000);//ms//延时1000ms,头文件在<string.h>
strlen(arr1)//strlen这个关键字代表sizeof(arr1)/sizeof(arr1[0])-1,头文件在<string.h>