PAT基础编程题目-6-3 简单求和
程序员文章站
2022-03-13 16:48:59
...
PAT基础编程题目-6-3 简单求和
题目详情
解答
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));
}
}
创作不易,喜欢的话加个关注点个赞,谢谢谢谢谢谢!