码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-08-01 22:47:02      阅读:279      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   strong   io   数据   

Sample input:
2
4 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 5
0 

 

 

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:
10
15

 

 

 

/ 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; 
} 

 

 

 

整型数据  字符串流

 

bubuko.com,布布扣
#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;
}

 

 

bubuko.com,布布扣

 

可以连续几行数,,,分别相加求和,布布扣,bubuko.com

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

标签:style   blog   http   color   os   strong   io   数据   

原文地址:http://www.cnblogs.com/2014acm/p/3885755.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!