请你求出数组元素之和。
public class Program {
public static void Main() {
// 读取第一行的 n
int n = int.Parse(System.Console.ReadLine());
// 读取第二行的数组元素
string[] tokens = System.Console.ReadLine().Split();
long sum = 0; // 使用 long 类型避免溢出
for (int i = 0; i < n; i++) {
sum += long.Parse(tokens[i]); // 转换为 long 类型
}
System.Console.WriteLine(sum);
}
} 数组元素之和的最大值可能达到 (10^9 * 10^5 = 10^14),这已经超出了 C# 中 int 类型的范围(最大值约为 (2*10^9))。import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextInt()) { // 注意 while 处理多个 case
int a = in.nextInt();
long b = 0;
while(0<a--){
b = b + in.nextInt();
}
System.out.println(b);
}
}
}