求十个数中最大值
#include<stdio.h>
#include<windows.h>
int Max()
{
int a[] = { 34, 54, 67, 89, 456, 7456 };
int num = sizeof(a) / sizeof(a[0]);
int i = 1;
int max = a[0];//最好不要引用其他数,(如0,负数)
for (; i < num; i++)
{
if (max < a[i]){
max =a[i];
}
}
return max;
}
int main()
{
printf("%d", Max());
system("pause");
return 0;
}
求十个数中第二大值
(有瑕疵,如果第一个数最大的,是求不出来的)
#include<windows.h>
int Max()
{
int a[] = { 34, 54, 67, 89, 456, 7456 };
int num = sizeof(a) / sizeof(a[0]);
int i = 1;
int max = a[0];//最好不要引用其他数,(如0,负数)
int second = a[0];
for (; i < num; i++)
{
if (max < a[i]){
second = max;
max =a[i];
}
}
printf("second:%d\n", second);
return max;
}
int main()
{
printf("%d", Max());
system("pause");
return 0;
}
本文介绍了一个使用C语言实现的程序,该程序能够从数组中找出最大值和次大值。通过遍历数组并比较元素,程序有效地确定了最大值,并通过额外的变量跟踪了第二大值。

被折叠的 条评论
为什么被折叠?



