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

字符串转化并累加

时间:2018-10-04 09:09:35      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:mic   ios   names   size   end   family   out   bsp   ros   

    一行字符类型的数字,每个数字以空格隔开,将它们转化为数字后累加求和

 

 1 #include <iostream>
 2 #include <string>
 3 #include <sstream>
 4 using namespace std;
 5 
 6 int main() {
 7     string str;
 8     while (getline(cin, str)) {
 9         int sum = 0, num = 0;
10         stringstream ssio(str);
11         while (ssio >> num)
12             sum += num;
13         cout << sum << endl;
14     }
15 
16     return 0;
17 }

 

字符串转化并累加

标签:mic   ios   names   size   end   family   out   bsp   ros   

原文地址:https://www.cnblogs.com/moujun1001/p/9741013.html

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