部分A+B(自定义函数修改)

题目描述
正整数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的值。
样例输入 Copy
3862767 6 13530293 3
3862767 1 13530293 8
样例输出 Copy
399
0

判断这,判断那的。

#include<bits/stdc++.h>
using namespace std;
int put(int len,int n,string m){//自定义一个函数,查找一段数中,一个数出现多少次
    int t=0;
    int q=0;
    for(int i=0;i<len;i++){
        if(m[i]-'0'==n){
            t++;
        }
    }
    return t; 
}
int pt(int w){//判断出现次数,返回多少位1
    if(w==0)
        return 0;
    if(w==1)
        return 1;
    if(w==2)
        return 11;
    if(w==3)
        return 111;
    if(w==4)
        return 1111;
    if(w==5)
        return 11111;
    if(w==6)
        return 111111;
    if(w==7)
        return 1111111;
    if(w==8)
        return 11111111;
    if(w==9)
        return 111111111;
}
int main(){
    int x,y;
    string x1;
    string y1;
    cin>>x1>>x>>y1>>y;
    int t=0,q=0;
    int lenx=x1.length();//求长度
    int leny=y1.length();
    int tx=put(lenx,x,x1),ty=put(leny,y,y1);//tx等于x1中x出现的次数....
    cout<<pt(tx)*x+pt(ty)*y;//比如2出现四次,就2*1111,四个1;
    return 0;
}

把自定义函数pt改改

#include<bits/stdc++.h>
using namespace std;
int put(int len,int n,string m){
	int t=0;
	int q=0;
	for(int i=0;i<len;i++){
		if(m[i]-'0'==n){
			t++;
		}
	}
	return t;
}
int pt(int w){
	int t=0;
	for(int i=0;i<w;i++){
		t*=10;
		t=t+1;
	}
	return t;
}
int main(){
	int x,y;
	string x1;
	string y1;
	cin>>x1>>x>>y1>>y;
	int t=0,q=0;
	int lenx=x1.length();
	int leny=y1.length();
	int tx=put(lenx,x,x1);
	int ty=put(leny,y,y1);
	cout<<pt(tx)*x+pt(ty)*y;
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

書盡

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

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

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

打赏作者

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

抵扣说明:

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

余额充值