【C语言刷题】数组
1.多个字符从两端移动,向中间汇聚
编写代码,演⽰多个字符从两端移动,向中间汇聚
比如:
//welcome to life!!!
//******************
//w****************!
//we**************!!
//wel************!!!
//.....
//welcome to life!!!
#include <stdio.h>
#include <string.h>
#include <Windows.h>
int main()
{
//char arr[] = { 'a','b','c' };//字符数组初始化方式1:单个字符
char arr1[] = "welcome to life!!!";//字符数组初始化方式2:字符串
char arr2[] = "******************";
int left = 0;//w的下标
int right = strlen(arr1) - 1;//!下标
//arr1:w e l c o m e t o l i f e !!!\0
//strlen(arr1):16,不包含\0
while (left <= right)
{
arr2[left] = arr1[left];//w
arr2[right] = arr1[right];//!
printf("%s\n", arr2);//w****************!
Sleep(1000);//休眠,单位:毫秒
system("cls");//清理,单行输出
left++;
right--;
}
printf("%s\n", arr2);
return 0;
}
2. 二分查找:在有序的数组中,查找指定的数值。
- 折半查找(二分查找速度很快),但是条件也苛刻,有序数组才能二分查找。
- 求中间元素的下标,使⽤ mid = (left + right) / 2;
如果left和right⽐较⼤的时候可能存在问题,可以使⽤:mid = left + (left + right) / 2;
(1)for循环从头往后遍历,挨个找。
(2)二分查找
在⼀个升序的数组中查找指定的数字n。
#include <stdio.h>
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int k = 0;
scanf("%d", &k);//键盘输入要查找的数字
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)
{
left = mid + 1;
}
else if (arr[mid] > k)
{
right = mid - 1;
}
else
{
printf("找到了,下标是:%d\n", mid);
break;
}
}
if (left > right)
printf("找不到了\n");
return 0;
}
查找12:
(3)找不到的时候,可以加一个flag== 0判断
#include <stdio.h>
int main()
{
int arr[] = { 1,2,3,4,5,6,7,8,9,10 };
int k = 0;
scanf("%d", &k);//键盘输入要查找的数字
int sz = sizeof(arr) / sizeof(arr[0]);
int left = 0;
int right = sz - 1;
int flag = 0;//假设找不到
while (left <= right)
{
int mid = (left + right) / 2;//被查找范围中间元素的下标
if (arr[mid] < k)
{
left = mid + 1;
}
else if (arr[mid] > k)
{
right = mid - 1;
}
else
{
printf("找到了,下标是:%d\n", mid);
flag = 1;//找到了
break;
}
}
if (flag == 0)
printf("找不到了\n");
return 0;
}
3. BC68-X形图案
牛客链接:BC68-X形图案
图解:
#include <stdio.h>
int main()
{
int n = 0;
while (scanf("%d", &n) != EOF)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i == j || (i + j == n - 1))
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
}
return 0;
}
4. BC69-空心正方形图案
#include <stdio.h>
int main()
{
int n = 0;
while(scanf("%d", &n) != EOF)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (j == 0 || j == n - 1)
{
printf("* ");
}
else if (i == 0 || i == n - 1)
{
printf("* ");
}
else
printf(" ");
}
printf("\n");
}
}
return 0;
}
5.【一维数组】BC92-逆序输出
#include <stdio.h>
int main()
{
int arr[10] = { 0 };
int i = 0;
for ( i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
for (i = 9; i >=0; i--)
{
printf("%d ", arr[i]);
}
return 0;
}
6.【二维数组】BC107-矩阵转置
#include <stdio.h>
int main()
{
int m, n;
int i = 0;
int j = 0;
int arr[][10] = { 0 };
while (scanf("%d%d", &n, &m) != EOF)
{
//存数据
for (i = 0; i < n; i++)
{
for (j = 0; j < m; j++)
{
scanf("%d", &arr[i][j]);
}
}
//转置
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
printf("%d ", arr[j][i]);
}
printf("\n");
}
}
return 0;
}
7.*【一维数组】BC100-有序序列合并
#include <stdio.h>
int main()
{
int n, m;
scanf("%d%d", &n, &m);
int arr1[1000] = { 0 };
int arr2[1000] = { 0 };
for (int i = 0; i < n; i++)
{
scanf("%d", &arr1[i]);
}
for (int j = 0; j < m; j++)
{
scanf("%d", &arr2[j]);
}
//合并
int i = 0;
int j = 0;
while (i < n && j < m)
{
if (arr1[i] < arr2[j])
{
printf("%d ", arr1[i]);
i++;
}
else
{
printf("%d ", arr2[j]);
j++;
}
}
//考虑特殊情况
while (i < n)
{
printf("%d ", arr1[i]);
i++;
}
while (j < m)
{
printf("%d ", arr2[j]);
j++;
}
return 0;
}
8.【一维数组】输入10个整数,求平均值
编写一个程序,从用户输入中读取10个整数并存储在一个数组中。然后,计算并输出这些整数的平均值。
#include <stdio.h>
int main()
{
int arr[10] = { 0 };
float sum = 0;
for (int i = 0; i < 10; i++)
{
scanf("%d",&arr[i]);
sum += arr[i];
}
double avg = sum / 10.0;
printf("%lf\n", avg);
return 0;
}
9.【一维数组】交换数组
将数组A中的内容和数组B中的内容进行交换。(数组一样大)
#include <stdio.h>
int main()
{
int arr1[10] = { 1,2,3,4,5,6,7,8,9,10 };
int arr2[10] = { 11,21,31,41,51,61,71,81,91,101 };
printf("交换之前:\n");
for (int i = 0; i < 10; i++)
{
printf("%d ", arr1[i]);
}
printf("\n");
for (int i = 0; i < 10; i++)
{
printf("%d ", arr2[i]);
}
for (int i = 0; i < 10; i++)
{
int tmp = arr1[i];
arr1[i] = arr2[i];
arr2[i] = tmp;
}
printf("\n");
printf("交换完成了:\n");
for (int i = 0; i < 10; i++)
{
printf("%d ", arr1[i]);
}
printf("\n");
for (int i = 0; i < 10; i++)
{
printf("%d ", arr2[i]);
}
return 0;
}