求最大值
题目内容
求10个整数中的最大值
思路
- 采用循环的方式输入一个数组
- 使用max标记数组中的最大值,采用循环的方式依次获取数组中的每个元素,与max进行比较,如果arr[i]大于 max,更新max标记的最大值,数组遍历结束后,max中保存的即为数组中的最大值。
代码实现
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
int main()
{
//求最大值
int arr[10] = { 0 };
int i = 0;
int max = 0;
printf("请输入10个整数:");
for (int i = 0; i < 10; i++)
{
scanf("%d", &arr[i]);
}
max = arr[0];
for (int i = 0; i < 10; i++)
{
if (arr[i] > max)
{
max = arr[i];
}
}
printf("%d", max);
结果

这篇博客介绍了一种使用C语言寻找10个整数数组中最大值的方法。通过循环遍历数组,利用变量max记录当前最大值,并在每次循环中与新元素比较,若新元素更大则更新max。最终,max变量将存储数组的最大值并打印出来。这个简单的算法适用于小规模数据的最大值查找。
290






