请你分别求出每个数组的元素之和。
num = int(input()) for i in range(num): length = int(input()) total = sum(int(x) for x in input().split()) print(total)Python 用不到数组(列表)长度,一行解决输入列表求和
#include <iostream>
using namespace std;
int main() {
int n_groups = 0;
int n_elem = 0;
std::cin >> n_groups;
while (n_groups--) {
std::cin >> n_elem;
long int elem_this , elem_next = 0;
for (int ind = 0;ind <= n_elem - 1;ind++) {
std::cin >> elem_this;
elem_next += elem_this;
}
std::cout << elem_next <<std::endl;
}
} public class Program {
public static void Main() {
string line;
int t = 0;
int n = 1;
long sum = 0;
while ((line = System.Console.ReadLine ()) != null) { // 注意 while 处理多个 case
string[] tokens = line.Split();
if (t == 0) //读取数组数量
t = int.Parse(tokens[0]);
else if (n == 1)//当前行为数组元素数量,跳过
n = -1;
else {
foreach (string x in tokens) {
sum += int.Parse(x);
}
System.Console.WriteLine(sum);
n = 1;//置1,跳过下一行的数组元素数量
sum = 0;//清0,以便下一个数组汇总
}
}
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
// 注意 hasNext 和 hasNextLine 的区别
int n = 0 ;
if(in.hasNextInt()){
n = in.nextInt();
}
for(int i=0;i<n;i++){
int num=0;
if(in.hasNextInt()){
num = in.nextInt();
}
long sum = 0;
for(int j=0;j<num;j++){
if(in.hasNextInt()) {
sum = sum +in.nextInt();
}
}
System.out.println(sum);
}
}
} import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int A = input.nextInt();
// 注意 hasNext 和 hasNextLine 的区别
while (A > 0) { // 注意 while 处理多个 case
A--;
int B = input.nextInt();
long sum = 0;
for(int i = 0; i < B; i++){
long a = input.nextInt();
sum += a;
}
System.out.println(sum);
}
}
}