A+B(6)
比赛主页
我的提交
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 262144K,其他语言524288K
64bit IO Format: %lld
题目描述
计算一系列数的和
打开以下链接可以查看正确的代码
https://ac.nowcoder.com/acm/contest/5657#question
输入描述:
输入数据有多组, 每行表示一组输入数据。
每行的第一个整数为整数的个数n(1 <= n <= 100)。
接下来n个正整数, 即需要求和的每个正整数。
输出描述:
每组数据输出求和的结果
示例1
输入
复制
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 n;
int x;
while (cin >> n)
{
int cur_sum = 0;
while (n --)
{
cin >> x;
cur_sum += x;
}
cout << cur_sum << endl;
}
return 0;
}2.python3 [int(x) for x in input().split()]
while True:
try:
nums = [int(x) for x in input().split()]
print(sum(nums[1: ]))
except:
break3.java hasNextInt() nextInt()
import java.util.*;
public class Main
{
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);
int n;
while (scan.hasNextInt())
{
n = scan.nextInt();
int cur_sum = 0;
int x;
while (n -- > 0)
{
x = scan.nextInt();
cur_sum += x;
}
System.out.println(cur_sum);
}
}
}
京公网安备 11010502036488号