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

PAT基础编程题目-6-3 简单求和

程序员文章站 2022-03-13 16:48:59
...

PAT基础编程题目-6-3 简单求和

题目详情

PAT基础编程题目-6-3 简单求和

题目地址:https://pintia.cn/problem-sets/14/problems/735

解答

C语言版

#include <stdio.h>

#define MAXN 10

int Sum(int List[], int N);

int main()
{
	int List[MAXN], N, i;

	scanf("%d", &N);
	for (i = 0; i < N; i++)
		scanf("%d", &List[i]);
	printf("%d\n", Sum(List, N));

	return 0;
}

int Sum(int List[], int N) {
	int sum = 0;
	for (int i = 0; i < N; i++)
		sum = sum + List[i];
	return sum;
}

PAT基础编程题目-6-3 简单求和

C++版

#include<iostream>
using namespace std;
#define MAXN 10
int Sum(int List[], int N);
int main() {
	int List[MAXN], N;
	cin >> N;
	for (int i = 0; i < N; i++)
		cin >> List[i];
	cout<<Sum(List, N);
	return 0;
}
int Sum(int List[], int N) {
	int sum = 0;
	for (int i = 0; i < N; i++)
		sum = sum + List[i];
	return sum;
}

PAT基础编程题目-6-3 简单求和

Java版

public class Main{
	
	private static final int MAXN = 10;
	
	private static int Sum (int [] List, int N) {
		int sum = 0;
		for (int i = 0; i < N; i++) {
			sum = sum +List[i];
		}
		return sum;
	}
	

	public static void main(String[] args) {
		int [] List = new int[MAXN];
		int N = 0;
		Scanner scanner = new Scanner(System.in);
		if(scanner.hasNext()) {
			N = scanner.nextInt();
			for (int i = 0; i < N; i++) {
				List[i] = scanner.nextInt();
			}
		}
		scanner.close();
		System.out.println(Sum(List, N));

	}

}

PAT基础编程题目-6-3 简单求和

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