文章

5

粉丝

153

获赞

0

访问

4.1k

头像
处理输入流中的“,”,把它变成一个string,然后转为int,再做A+B
P1335 浙江大学机试题
发布于2023年6月24日 11:51
阅读数 471

#include <iostream>
#include <string>
#include <algorithm>

using namespace std;

int main() {
    string line;
    // 使用getline函数读取一行数据到line字符串中
    while (getline(cin, line)) {
        // 使用remove函数去除字符串中所有的逗号(','),然后使用erase函数删除这些逗号
        line.erase(remove(line.begin(), line.end(), ','), line.end());

        // 找到空格的位置,将字符串line按照空格位置分割为两个子字符串strA和strB
        int spacePos = line.find(' ');
        string strA = line.substr(0, spacePos);
        string strB = line.substr(spacePos + 1);

        // 使用stoi函数将子字符串strA和strB转化为整数A和B
        int A = stoi(strA);
        int B = stoi(strB);

        // 输出两个整数的和
        cout << A + B << endl;
    }

    return 0;
}

 

登录查看完整内容


登录后发布评论

暂无评论,来抢沙发