部分A+B

来源:互联网 发布:图像制作软件 编辑:程序博客网 时间:2024/04/29 11:24

    正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA。例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6。现给定A、DA、B、DB,请编写程序计算PA + PB

   

#include<iostream>#include<string>using namespace std;int sumAandB(string strA, int DA, string strB, int DB){int sumA = 0;int sumB = 0;for (int i = 0; i < strA.size(); i++){char tmp = strA[i];if ((tmp - '0') == DA)sumA = sumA * 10 + (tmp - '0');}for (int i = 0; i < strB.size(); i++){char tmp = strB[i];if ((tmp - '0') == DB)sumB = sumB * 10 + (tmp - '0');}return sumA + sumB;}int main(){string strA, strB;int DA, DB;cin >> strA >> DA >> strB >> DB;cout << sumAandB(strA, DA, strB, DB) << endl;cin.get();cin.get();}


0 0
原创粉丝点击