欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PAT基础编程题目-6-5 求自定类型元素的最大值

程序员文章站 2022-03-13 13:46:28
...

PAT基础编程题目-6-5 求自定类型元素的最大值

题目详情

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;
}

PAT基础编程题目-6-5 求自定类型元素的最大值

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;
}

PAT基础编程题目-6-5 求自定类型元素的最大值

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)));
	}

}

PAT基础编程题目-6-5 求自定类型元素的最大值

创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢谢!