calculate the sum of some integers

Description

Your task is to calculate the sum of some integers.

Input

Input contains an integer N in the first line, and then N lines follow. Each
line starts with a integer M, and then M integers follow in the same line.

Output

For each group of input integers you should output their sum in one line, and
with one line of output for each line in input.

Sample Input

2
4 1 2 3 4
5 1 2 3 4 5

Sample Output

10


#include<iostream>
using namespace std;

int main()
{
int N1=0,x=0;
int **arrayN;
cin>>N1;
arrayN=new int*[N1];
for(int i=0;i<N1;i++)
{
cin>>x;
arrayN[i]=new int [x+1];
for(int j=1;j<x+1;j++)
{
cin>>arrayN[i][j];
}
arrayN[i][0]=x;
}

int *sum=new int [N1];
for(int i=0;i<N1;i++)
{
sum[i]=0;
}
for(int i=0;i<N1;i++)
{
int k=arrayN[i][0];
for(int j=1;j<k+1;j++)
{
sum[i]+=arrayN[i][j];
}
}
for(int i=0;i<N1;i++)
{
cout<<sum[i]<<endl;
}
return 0;
}

 

calculate the sum of some integers

时间: 2024-08-29 08:25:00

calculate the sum of some integers的相关文章

371. Sum of Two Integers

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example: Given a = 1 and b = 2, return 3. Credits: Special thanks to @fujiaozhu for adding this problem and creating all test cases. 这道题让我们实现两数相加,但是不能用加号或

[leetcode] 371. Sum of Two Integers 解题报告

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example:Given a = 1 and b = 2, return 3. 用 异或 表示sum,与 表示 carry,carry左移一位后,递归调用getSum(sum,carry<<1) 一刷: public int getSum(int a, int b) { return b ==0 ? a

LeetCode 371 Sum of Two Integers

This problem is a little confusing to me at first. But after I read some articles online, I got to know that it requires bitwise operations. So basically, we need to use "^" to calculate the sum of two integers, and "&" << 1

371. Sum of Two Integers【位运算】

2017/3/16 20:03:07 Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example:Given a = 1 and b = 2, return 3. 思路: 1.^可以得到哪些bit求和后应该是1.(进位的bit应该变成0,1^1=0) 2.&可以得到哪些bit应该进位 3.由于进位后的bit是1可能和第一步得到的1再次产生进位,所以不

371.用位运算实现加法 Sum of Two Integers

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example:Given a = 1 and b = 2, return 3. Credits:Special thanks to @fujiaozhu for adding this problem and creating all test cases. Subscribe to see which

【leetcode】371. Sum of Two Integers

题目描述: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. 解题分析: 这种类型的题必然要用位运算,虽然自己写了关于位运算的代码,但是不够简洁. 后来参考了这篇博文: http://blog.csdn.net/zhongjiekangping/article/details/6855864 这篇博文对位运算加法的实现讲的得十分清楚,我这里就只放按此思路写

Leetcode 371. Sum of Two Integers JAVA语言

Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. Example: Given a = 1 and b = 2, return 3. 题意:计算a+b,但是不许使用+和- public class Solution {     public int getSum(int a, int b) {         //第一种         while(a!=

Nim Game,Reverse String,Sum of Two Integers

下面是今天写的几道题: 292. Nim Game You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take t

**leetcode笔记--4 Sum of Two Integers

question: Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. my wrong answer 错误点: 显示memory error 应该是运行较大数字时导致内存不够,所以这种方法不佳 正确答案地址:(暂时不明白) http://blog.csdn.net/mebiuw/article/details/51788817 http://www.cnb