首页 > 代码库 > 可以连续几行数,,,分别相加求和

可以连续几行数,,,分别相加求和

Sample input:24 1 2 3 4 5 1 2 3 4 5 Sample output:    10                         15       // C语言AC代码#include <stdio.h> int n,c,t,sum; int main() {     while( scanf("%d",&c)!=EOF )       {   while(c--)                {   scanf("%d",&n); sum=0;                         while(n--)                              {    scanf("%d",&t);                            sum+=t;   }             printf("%d\n",sum); }       }        return 0;}      // C++语言AC代码#include <iostream>using namespace std;int n,c,t,sum; int main() {     while( cin>>c)          {    while(c--)                     {   cin>>n;     sum=0;                         while(n--)                                {    cin>>t;  sum+=t;   }             cout<<sum<<endl; }       }        return 0;}      ********************************************************************************************************************************************    Sample input:4    1 2 3 4 5     1 2 3 4 50   Sample output:    10                         15  // C语言AC代码#include<stdio.h>   int main() {   int i,n,a[100],m=0;               while( scanf("%d",&n)!=EOF  &&  (n!=0) )         {              for(i=0;i<n;i++)                      {   scanf("%d",&a[i]);       m+=a[i];   }                     printf("%d\n",m);         m=0;      }      return 1;}     / C++语言AC代码#include <iostream>using namespace std;int n,sum,t;int main(){   while( cin>>n && n)     {   sum=0;         while(n--)             {       cin>>t;       sum+=t;   }         cout<<sum<<endl;     }     return 0;}       ***************************************************************************************************Sample input:4 1 2 3 4 5 1 2 3 4 5 Sample output:1015   / C++语言AC代码#include <iostream>using namespace std;int n,sum,t; int main() {   while( cin>>n && n )             {     sum=0;            while(n--)             {     cin>>t;   sum+=t;  }               cout<<sum<<endl;      }     return 0; } 

 

 

 

整型数据  字符串流

 

#include <iostream>#include <string>#include <sstream>using namespace std;int main( ){    string s;    int x;    int sum;    while (getline(cin, s))    {        stringstream ss(s);        sum=0;        while (ss>>x) sum=sum+x;        cout<<sum<<endl;    }    return 0;}
View Code

#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main( )
{
    string s;
    int x;
    int sum;
    while (getline(cin, s))
    {
        stringstream ss(s);
        sum=0;
        while (ss>>x) sum=sum+x;
        cout<<sum<<endl;
    }
    return 0;
}