展开全部
一、算法思62616964757a686964616fe78988e69d8331333363373138想
假设N个数存储在一维数组中,令第一个数为最大值,从第二个数开始逐个和当前最大值进行比较,若比当前最大值大,则用它替换当前最大值。最后保存的最大值即为N个数中的最大值。
二、操作过程49 38 65 97 76 13 27 49 max = 49
^
49 38 65 97 76 13 27 49 max = 65
^
49 38 65 97 76 13 27 49 max = 97
^
49 38 65 97 76 13 27 49 max = 97
^
49 38 65 97 76 13 27 49 max = 97
^
49 38 65 97 76 13 27 49 max = 97
^
49 38 65 97 76 13 27 49 max = 97
^
max = 97
三、程序代码#include
using namespace std;
//返回数组中元素的最大值
//arr:数组
//n:数组大小
//返回n个数组元素的最大值
int max(int arr[], int n)
{
int maxValue = arr[0];
for(int i=1; i
{
if(arr[i] > maxValue)
{
maxValue = arr[i];
}
}
return maxValue;
}
int main()
{
int arr[] = {49, 38, 65, 97, 76, 13, 27, 49};
int n = 8;
cout<
return 0;
}
四、运行测试