1016. 部分A+B (15)

来源:互联网 发布:知乎 螳臂当车 编辑:程序博客网 时间:2024/05/17 21:51

正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA。例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6。

现给定A、DA、B、DB,请编写程序计算PA + PB

输入格式:

输入在一行中依次给出A、DA、B、DB,中间以空格分隔,其中0 < A, B < 1010

输出格式:

在一行中输出PA + PB的值。

输入样例1:
3862767 6 13530293 3
输出样例1:
399
输入样例2:
3862767 1 13530293 8
输出样例2:
0
#include "iostream"#include "cmath"using namespace std;int main(){long A, B;int DA, DB, PA, PB;cin >> A >> DA >> B >> DB;int countA = 0, countB = 0;while (A > 0){if (DA == A % 10)         //按位相比,有相等就计数加一countA++;A /= 10;}while (B > 0){if (DB == B % 10)countB++;B /= 10;}if (countA == 0)PA = 0;else if (countA == 1)PA = DA;else{PA = DA;for (int i = 1; i < countA; i++){PA = DA*(int)pow(10, i) + PA;}}if (countB == 0)PB = 0;else if (countB == 1)PB = DB;else{PB = DB;for (int i = 1; i < countB; i++){PB = DB*(int)pow(10, i) + PB;//PA=0;PA=PA*10+DA;}}cout << PA + PB << endl;return 0;}
</pre><pre name="code" class="cpp">Adem:简单..
</pre><pre name="code" class="cpp"><pre name="code" class="cpp">#include <iostream>#include <string>using namespace std;int work(string a,int da){    int i,temp = 0;    for(i=0;i<a.length();i++)        if(a[i]==da+48)            temp = temp*10+da;    return temp;}int main(){    string a,b;    int da,db,pa,pb;    cin>>a>>da>>b>>db;    pa = work(a,da);    pb = work(b,db);    cout<<pa+pb<<endl;    system("pause");    return 0;}




                                             
0 0
原创粉丝点击