1. 程式人生 > 其它 >6-5 求自定型別元素的最大值 (10 分)

6-5 求自定型別元素的最大值 (10 分)

本題要求實現一個函式,求N個集合元素S[]中的最大值,其中集合元素的型別為自定義的ElementType

函式介面定義:

ElementType Max( ElementType S[], int N );

其中給定集合元素存放在陣列S[]中,正整數N是陣列元素個數。該函式須返回NS[]元素中的最大值,其值也必須是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
ElementType Max( ElementType S[], int N )
{
    ElementType a;
    
int j; a=S[0]; for(j=0;j<N;j++) { if(a>=S[j]) a=a; else a=S[j]; } return a; }