PAT基础编程题目-6-5 求自定类型元素的最大值
题目详情
题目地址:https://pintia.cn/problem-sets/14/problems/737
解答
C语言版
#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;
}
ElementType
Max(ElementType S
[], int N
) {
ElementType max
= S
[0];
for (int i
= 1; i
< N
; i
++)
if (max
< S
[i
])
max
= S
[i
];
return max
;
}
C++版
#include<iostream>
#include<iomanip>
using namespace std
;
#define MAXN 10
typedef float ElementType
;
ElementType
Max(ElementType S
[], int N
);
int main() {
ElementType S
[MAXN
];
int N
;
cin
>> N
;
for (int i
= 0; i
< N
; i
++)
cin
>> S
[i
];
cout
<< fixed
<< setprecision(2) << Max(S
, N
);
return 0;
}
ElementType
Max(ElementType S
[], int N
) {
ElementType max
= S
[0];
for (int i
= 1; i
< N
; i
++)
if (max
< S
[i
])
max
= S
[i
];
return max
;
}
Java版
public class Main{
private static final int MAXN
= 10;
private static float Max(float []S
, int N
) {
float max
= S
[0];
for (int i
= 1; i
< N
; i
++) {
if(max
< S
[i
])
max
= S
[i
];
}
return max
;
}
public static void main(String
[] args
) {
int N
=0;
float [] S
= new float[MAXN
];
Scanner scanner
= new Scanner(System
.in
);
if (scanner
.hasNext()) {
N
= scanner
.nextInt();
for (int i
= 0; i
< N
; i
++) {
S
[i
] = scanner
.nextFloat();
}
}
scanner
.close();
DecimalFormat decimalFormat
= new DecimalFormat("#.00");
System
.out
.println(decimalFormat
.format(Max(S
, N
)));
}
}
创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢谢!
转载请注明原文地址:https://ipadbbs.8miu.com/read-54876.html