C语言学习笔记09

#define _CRT_SECURE_NO_WARNINGS 1

#include 
#include 
#include 

int main()
{
     
	int arr[] = {
      1,2,3,4,5,6,7,8,9,10 };
	int k = 7;
	//写一个代码,在arr数组(有序的)中找到7
	int i = 0;
	int sz = sizeof(arr) / sizeof(arr[0]);
	for (i = 1; i <= sz; i++)
	{
     
		if (arr[i] == 7)
		{
     
			printf("%d\n", i);
			break;
		}
		else
		{
     
			printf("无");
			break;
		}
	}

	//折半查找算法,二分查找算法
	int left = 0;  //左下标
	int right = sz - 1;  //右下标
	while(left<=right)
	{
     	
		int mid = (left + right) / 2;
		if (arr[mid] > k)
		{
     
			right = mid - 1;
		}
		else if (arr[mid] < k)
		{
     
			left = mid + 1;
		}
		else
		{
     
			printf("%d\n", mid);
			break;
		}
		if (left > right)
		{
     
			printf("找不到了");
		}
	}

	//动态显示文字
	char arr1[] = "welcome to bit!!!";
	char arr2[] = "#################";
	int sz = sizeof(arr1) / sizeof(arr1[0]);
	int i = 0;
	int left = 0;
	int right = sz - 2;  //或者=strlen(arr)-1
	for (i = 0; i < sz/2; i++)
	{
     
		arr2[left] = arr1[left];
		arr2[right] = arr1[right];
		printf("%s\n", arr2);
		Sleep(1000);  //休息一秒,函数单位ms,头文件windows.h
		system("cls");  //执行系统命令的函数 - cls - 清空屏幕
		left++;
		right--;
	}

	//用户输入密码,只能输入三次
	int i = 0;
	char password[20] = {
      0 };
	for (i = 0; i < 3; i++)
	{
     
		printf("请输入密码:>");
		scanf("%s", password);
		if (strcmp(password,"123456") == 0)  //==不能用来判断两个字符串相等,应该使用一个库函数 - strcmp
		{
     
			printf("登录成功\n");
			break;
		}
		else
		{
     
			printf("密码错误!\n");
		}
	}
	if (i == 3)
		printf("三次密码均错误,退出程序\n");

	return 0;
}

你可能感兴趣的:(C语言学习,c语言)