请你分别求出每个数组的元素之和。
第一行有一个整数。
随后组数据。
每组的第一行有两个整数和
。
每组的随后行,每行有
个整数
。
保证。
输出行,每行一个整数,代表数组元素之和。
3 3 4 1 2 3 4 5 6 7 8 9 10 11 12 1 1 2024 3 2 1 1 4 5 1 4
78 2024 16
public class Program { public static void Main() { string line; int t = int.Parse(System.Console.ReadLine ()); //读取组数 int x = 0; long sum = 0; if (t > 0) { //控制读取组 for (int i = 0; i < t; i++) { string[] tokens = System.Console.ReadLine ().Split(); x = int.Parse(tokens[0]);//行数 //控制读取组内 for (int j = 0; j < x; j++) { tokens = System.Console.ReadLine ().Split(); foreach (string s in tokens) { sum += int.Parse(s); } } //输出这个数组的和 System.Console.WriteLine(sum); sum = 0; x = 0; } } else System.Console.WriteLine(sum); } }
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 t = 0; if (in.hasNextInt()){ t = in.nextInt(); } while(0 < t--){ int n = 0; int m = 0; if(in.hasNextInt()){ n = in.nextInt(); } if(in.hasNextInt()){ m = in.nextInt(); } long sum = 0; int count = 0; count = n*m; while(0<count--){ if(in.hasNextInt()){ sum += in.nextInt(); } } System.out.println(sum); } in.close(); } }
#include <stdio.h> int main() { int m, n; long t,a; scanf("%ld",&t); for (int i=0;i<t;i++) { long sum = 0; scanf("%d%d",&m,&n); for (int j=0;j<m;j++){ for(int k=0;k<n;k++){ scanf("%ld",&a); sum+=a; } } printf("%ld\n",sum); sum = 0; } }