首页 > 试题广场 >

多组_一维数组_T组形式

[编程题]多组_一维数组_T组形式
  • 热度指数:20549 时间限制:C/C++ 3秒,其他语言6秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
给定 t 组询问,每次询问给出一个长度为 n 的正整数数组 a ,第 i 个元素的值为 a_i
请你分别求出每个数组的元素之和。

输入描述:
第一行有一个整数 t\ (\ 1 \leq t \leq 10^5\ )
随后 t 组数据。
每组的第一行有一个整数 n\ (\ 1 \leq n \leq 10^5\ )
每组的第二行有 n 个整数 a_i\ (\ 1 \leq a_i \leq 10^9\ )
保证 \sum n \leq 10^5


输出描述:
输出 t 行,每行一个整数,代表数组元素之和。
示例1

输入

3
3
1 4 7
1
1000
2
1 2

输出

12
1000
3
import java.util.Scanner;
import java.io.*;
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int t = Integer.parseInt(br.readLine());
for(int i = 0 ; i < t;i++)
{
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
long sum = 0;
for(int j = 0 ;j < n ; j++)
{
sum += Long.parseLong(st.nextToken());
}
sb.append(sum).append("\n");
}
System.out.println(sb.toString());
}
}
发表于 2025-08-09 12:52:00 回复(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);
        }  
    }
}

发表于 2025-04-05 12:23:42 回复(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 i = in.nextInt();
        while (0<i--) { // 注意 while 处理多个 case
            int a = in.nextInt();
            long b = 0;
            while(0<a--){
                b = b + in.nextInt();
            }
            System.out.println(b);
        }
    }
}

发表于 2024-11-19 15:54:27 回复(0)
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int a=in.nextInt();
        long sum=0;
        for(int b=0;b<a;b++){
            int d=in.nextInt();

            for(int c=0;c<d;c++){
                int x=in.nextInt();
                sum=x+sum;
            }
            System.out.println(sum);
            sum=0;
        }
    }
}

发表于 2024-11-13 18:06:44 回复(0)
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        //t组数据
        int t = in.nextInt();
        int i = 0;
        while (i++ < t) {
            //n个整数
            int n = in.nextInt();
            int j = 0;
            //sum统计整数之和(long类型防止结果溢出)
            long sum = 0;
            while (j++ < n){
                sum += in.nextInt();
            }
            System.out.println(sum);
        }
    }
}
发表于 2024-09-26 14:20:29 回复(0)
public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        long num = in.nextLong();
        for(long i = 0; i<num;i++){
            long every = in.nextLong();
            long eOut = 0;
            while(in.hasNextLong()){
                eOut = eOut + in.nextLong();
                every--;
                if(every == 0){
                    break;
                }
            }
            System.out.println(eOut);
        }
        in.close();
    }

发表于 2024-09-14 22:08:48 回复(0)