week5-[字符数组]数和
week5-[字符数组]数和
题目描述
输入一篇文章,单词之间由空格隔开,每个单词要么只包含字母,要么只包含数字。计算所有只包含数字的单词的和。
输入格式
输入共 111 行 111 个只包含字母,数字和空格的字符串。
输出格式
输出共 111 行 111 个整数表示答案。
样例 #1
样例输入 #1
hello 123 world 4567 final words
样例输出 #1
4690
提示
数据范围
对于所有数据,字符串长度不超过 100010001000,每个数都不超过 100001000010000。
#include <bits/stdc++.h>
using namespace std;int main() {string line;getline(cin, line);stringstream ss(line);string word;long long sum = 0;while (ss >> word) {bool isNum = true;for (char ch : word) {if (!isdigit(ch)) {isNum = false;break;}}if (isNum) sum += stoi(word); // 转换成整数累加}cout << sum << "\n";return 0;
}