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

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

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

	}

}

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

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务