PTA乙级1016(c++和python3)

PTA乙级1016(c++和python3)

题目信息:

正整数 A 的“DA(为 1 位整数)部分”定义为由 A 中所有 D​A组成的新整数 P​A。例如:给定 A=3862767,D​A=6,则 A 的“6 部分”P​A 是 66,因为 A 中有 2 个 6。现给定 A、D​A​​ 、B、DB,请编写程序计算 P​A+PB​​ 。
输入格式:
输入在一行中依次给出 A、D​A、B、D​B中间以空格分隔,其中 0<A,B<10*10。
输出格式:
在一行中输出 P​A+​PB的值。

python3

list1 = list(map(str, input().strip().split()))

a = int(list1[1])
b = int(list1[3])

count1 = ""
for i in list1[0]:
    if int(i) == a:
        count1 += i

count2 = ""
for i in list1[2]:
    if int(i) == b:
        count2 += i

if count1 != "" and count2 != "":
    print(int(count1) + int(count2))
elif count1 == "" and count2 != "":
    print(count2)
elif count1 != "" and count2 == "":
    print(count1)
elif count1 == "" and count2 == "":
    print(0)

c++

#include <iostream>
#include <string>
using namespace std;


int main()
{
    int a,b,c,d;
    cin>>a>>b>>c>>d;
    string a1 = to_string(a);
    string b1 = to_string(c);
    string e = "";
    string f = "";
    
    for(int i = 0; i < a1.length(); i++)
    {
        
        int aa1 = a1[i] - '0';
       if(aa1 == b)
        {
            e += a1[i];
        }
    }

    for(int i = 0; i < b1.length(); i++)
    {
        int bb1 = b1[i] - '0';
     
        if(bb1 == d)
        {
          
            f += b1[i];
        }
    }
    int result1 = 0;
    int result2 = 0;
    
   
    if(e != "")
    result1 = stoi(e);
   
    if(f != "")
    result2 = stoi(f);
    
    cout<<result1 + result2<<"\n";

    system("pause");//测试用
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

嘻嘻哈哈笑呵呵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值