1. #include <stdio.h>\\折半查找
#include <stdlib.h>
#include <string.h>
int main(){
int arr[10]={1,2,3,4,5,6,7,8,9,10};
int k=5;
int left=arr[0];
int right=sizeof(arr)/sizeof(arr[0])-1;
while(left<=right)
{
int mid=left+(right-left)/2;
if (arr[mid]==k){
printf("找到了,下标为:%d\n",mid);
break;
}
else if(arr[mid]<=k){
left=mid+1;
}
else{
right=mid-1;
}
}
if (left>right){
printf("没有找到!!\n");
}
system("pause");
return 0;
}
2.小型的密码识别程序
#include <stdio.h>
#include <string.h>
int main()
{
int i=0;
char password[20]={0};//定义密码为123456789
for(i=0;i<=3;i++){
printf("请输入密码:\n");
scanf("%s",&password);
if(0==strcmp(password,"123456789"))
{
break;
} else
{
printf("密码错误,请重新输入!\n");
}
}
if(i<=3){
printf("登陆成功!\n");
}
else{
printf("退出登录");
}return 0;
}
4.字符转换
#include <stdio.h>
#include <stdlib.h>
int main(){
int ch;
printf("请任意输入一个字符:\n");
while((ch=getchar())!=EOF)
{
if(ch>='a'&&ch<='z')
{
printf("%c\n",ch-32);
}else if(ch>='A'&&ch<='Z'){
printf("%c\n",ch+32);
}else if(ch>='0'&&ch<='9')
{
printf("\n");
}
system("pause");
return 0;
}
}