4-5 求自定類型元素的最大值

Attention: 如果喜歡我寫的文章,歡迎來我的github主頁給star
Githubgithub.com/MuziJin

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

函數接口定義:
ElementType Max( ElementType S[], int N );

其中給定集合元素存放在數組S[]中,正整數N是數組元素個數。該函數須返回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];
}

轉載請注明出處:github.com/MuziJin

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容