1.二分法找数
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int main()
{
int arr[]={1,2,3,4,5,6,7,8,9,10};
int k=7;
int sz=sizeof(arr)/sizeof(arr[0]);
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("找不到\n");
}
return 0;
}
2.从两边向中间缓慢打印一串数
#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<string.h>
#include<windows.h>
#include<stdlib.h>
int main()
{
char arr1[]="wo shi ni die!!!!!";
char arr2[]="##################";
int left=0;
int right=strlen(arr1)-1;
while(left<=right)
{
arr2[left]=arr1[left];
arr2[right]=arr1[right];
printf("%s\n",arr2);
Sleep(1000);
system("cls");
left++;
right--;
}
printf("%s\n",arr2);
return 0;
}
3.简易验证密码
int main()
{
int i=0;
char password[20]={0};
for(i=0;i<3;i++)
{
printf("请输入密码:");
scanf("%s",password);
if(strcmp(password,"123456")==0)
{
printf("登录成功\n");
break;
}
else{printf("密码错误\n");}
}
if (i==3)
printf("三次密码均错误\n");
return 0;
}