一、打印出被屏蔽的字符串
#include<stdio.h>
#include<string.h>
int main() {
char arr1[] = "welcome to bit!!!!!!";
char arr2[] = "####################";
int left = 0;
int right = strlen(arr1) - 1;
for (int i = 0; left < right; i++) {//可以用while循环
arr2[left] = arr1[left];
arr2[right] = arr1[right];
left++;
right--;
printf("%s\n", arr2);
}
}
结果如下:
(1)为了体现逐渐打印的过程,使用Sleep函数
#include<windows.h>
Sleep(1034)//睡眠1034毫秒
#include<stdio.h>
#include<string.h>
#include<Windows.h>
int main() {
char arr1[] = "welcome to bit!!!!!!";
char arr2[] = "####################";
int left = 0;
int right = strlen(arr1) - 1;
for (int i = 0; left < right; i++) {//可以用while循环
arr2[left] = arr1[left];
arr2[right] = arr1[right];
left++;
right--;
printf("%s\n", arr2);
Sleep(1000);
}
}
结果会逐行向下打印
(2)如果在一行内逐个打印字符
#include<stdio.h>
#include<string.h>
#include<Windows.h>
#include<stdlib.h>
int main() {
char arr1[] = "welcome to bit!!!!!!";
char arr2[] = "####################";
int left = 0;
int right = strlen(arr1) - 1;
for (int i = 0; left < right; i++) {
arr2[left] = arr1[left];
arr2[right] = arr1[right];
printf("%s\n", arr2);
left++;
right--;
Sleep(1000);
system("cls");//system是库函数,cls是清除控制台信息,类似linux命令中的clear
}
}
二、升序数组的二分查找
int main() {
//二分查找,在升序数组中
int arr[] = { 11,23,45,56,67,89,790,1234 };//8长度
int left = 0;
int right=sizeof(arr)/sizeof(arr[0]) - 1;
//如果是char类型数组,这里right要
//sizeof(arr)/sizeof(arr[0])-2,因为char类型数组末尾还有个\0
int k = 0;
scanf("%d", &k);
while (left <= right) {
int mid = (left + right) / 2;
if (k > arr[mid]) {
left = mid + 1;
}
else if(k<arr[mid]){
right = mid - 1;
}
else {
printf("%d", mid);
break;
}
}
return 0;
}