1016 部分A+B(C++)

思路:

1.分别找到A和B中PA和PB

2.利用关系式计算值

针对1:可以将A储存到字符串或者字符数组中,遍历查找整型PA或PB

针对2:找到关系式a = a * 10 + tempA

tempA:初始输入的PA    a:最终计算的值

C++和C代码类似,C代码就是将字符串换成字符数组,迭代器遍历换成下标遍历。

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string A, B;
    int a = 0, b = 0, tempA = 0, tempB = 0;
    cin >> A >> tempA >> B >> tempB;
    for (auto i = A.begin(); i != A.end(); ++i) {
        if (*i == char('0' + tempA))
            a = a * 10 + tempA;
    }
    for (auto i = B.begin(); i != B.end(); ++i) {
        if (*i == (char)('0' + tempB))
            b = b * 10 + tempB;
    }
    cout << a + b;

    return 0;
}

 

posted @ 2019-02-22 23:58  Hk_Mayfly  阅读(217)  评论(0)    收藏  举报