注册 登录
编程论坛 C++教室

请高手指点

谌俭 发布于 2008-10-14 01:14, 432 次点击
Problem 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
15


我用C++写的程序,请高手修改一下,我就是那个输入不懂怎么写,并说明下思路,谢谢各位了
#include "iostream"
using namespace std;
int main()
{
    int N,M;
    int i;
    cin>>N;
    while(N--)
    {
        int sum=0;
        cin>>M;
        for(i=0;i<=M;i++)
        {
            sum+=i;
        }
        cout<<sum<<endl;
    }

    return 0;
}
2 回复
#2
larden2008-10-14 03:00
楼主想修改什么?
程序里的N变量是让while语句执行N-1次,比如你先输入3,后面的while语句就调用2次,
在输入4就是1+2+3+4=10,输出10,再输入5就是1+2+3+4+5=15 程序没错,哪里看不懂么?
#3
谌俭2008-10-14 16:46
就是输入怎么实现,如果是4,那么后面的1,2,3,4用什么样的方法输入呢??
1