题解 | #OJ在线编程常见输入输出练习场--A+B(5)#c++/python3/java
A+B(5)
https://ac.nowcoder.com/acm/contest/5657/E
A+B(5)
比赛主页
我的提交
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld
题目描述
计算一系列数的和
打开以下链接可以查看正确的代码
https://ac.nowcoder.com/acm/contest/5657#question
输入描述:
输入的第一行包括一个正整数t(1 <= t <= 100), 表示数据组数。
接下来t行, 每行一组数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。
输出描述:
每组数据输出求和的结果
示例1
输入
复制
2
4 1 2 3 4
5 1 2 3 4 5
输出
复制
10
15
思路和心得
1.c++ cin>>
#include <bits/stdc++.h>
using namespace std;
int main()
{
int T; cin >> T;
while (T --)
{
int n; cin >> n;
int cur_sum = 0;
int x;
while (n --)
{
cin >> x;
cur_sum += x;
}
cout << cur_sum << endl;
}
return 0;
}2.pyhton3 [int(x) for x in input().split()]
T = int(input())
for _ in range(T):
nums = [int(x) for x in input().split()]
print(sum(nums[1: ]))
3.java hasNextInt() nextInt()
import java.util.*;
public class Main
{
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
while (T -- > 0)
{
int n = scan.nextInt();
int cur_sum = 0;
int x;
while (n -- > 0)
{
x = scan.nextInt();
cur_sum += x;
}
System.out.println(cur_sum);
}
}
}
查看8道真题和解析