Attention: 如果喜歡我寫的文章,歡迎來我的github主頁給star
Github:github.com/MuziJin
本題要求實現(xiàn)一個函數(shù),求N個集合元素S[]中的最大值,其中集合元素的類型為自定義的ElementType。
函數(shù)接口定義:
ElementType Max( ElementType S[], int N );
其中給定集合元素存放在數(shù)組S[]中,正整數(shù)N是數(shù)組元素個數(shù)。該函數(shù)須返回N個S[]元素中的最大值,其值也必須是ElementType類型。
裁判測試程序樣例:
#include <stdio.h>
#define MAXN 10
typedef float ElementType;
ElementType Max( ElementType S[], int N );
int main ()
{
ElementType S[MAXN];
int N, i;
scanf("%d", &N);
for ( i=0; i<N; i++ )
scanf("%f", &S[i]);
printf("%.2f\n", Max(S, N));
return 0;
}
輸入樣例:
3
12.3 34 -5
輸出樣例:
34.00
Code
ElementType Max( ElementType S[], int N )
{ ElementType temp;
for(int i=1; i<N; i++)
{
if(S[i] <S[i-1])
{
temp = S[i];
S[i] = S[i-1];
S[i-1] = temp;
}
}
return S[N-1];
}
轉(zhuǎn)載請注明出處:github.com/MuziJin