首页 > 试题广场 >

1016. 部分A+B (15)

[编程题]1016. 部分A+B (15)
  • 热度指数:29777 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
正整数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

输出

399
很傻的方法,初学者的思路

#include <iostream>
using namespace std;
long PA(long A,long DA)
{
    int i;
    int count=0;
    long PA=0;
    for(i=A;i!=0;i/=10)
    {
        if(i%10==DA)
        {
            count++;
        }
    }
    for(i=count;i>0;i--)
    {
        PA*=10;
        PA+=DA;
    }
    return PA;
}
int main()
{
    long A,DA,B,DB;
    cin>>A>>DA>>B>>DB;
    cout<<PA(A,DA)+PA(B,DB);
}

发表于 2019-07-07 10:13:41 回复(0)

分析

,int正数最大为20亿左右,所以不能用int,需要long long类型。

/*
 * app=PAT-Basic lang=c++
 * https://pintia.cn/problem-sets/994805260223102976/problems/994805306310115328
 */
#include <cstdio>
int main()
{
    long long A, B,PA = 0,PB = 0;
    int DA, DB;
    scanf("%lld%d%lld%d",&A,&DA,&B,&DB);
    while (A != 0){
        if ((A % 10) == DA){
            PA *= 10;
            PA += DA;
        }
        A /= 10;
    }
    while (B != 0){
        if ((B % 10) == DB){
            PB *= 10;
            PB += DB;
        }
        B /= 10;
    }
    printf("%lld",PA+PB);
    return 0;
}

编辑于 2019-12-02 13:54:57 回复(0)
import java.util.Scanner;
 
public class Main {
 
    public static void main(String[] args) {
        Scanner scanner = newScanner(System.in);
        int A = scanner.nextInt();
        int Da = scanner.nextInt();
        int B = scanner.nextInt();
        int Db = scanner.nextInt();
 
        int pa = calculateP(A, Da);
        int pb = calculateP(B, Db);
        System.out.println(pa + pb);
 
        scanner.close();
    }
 
    private static int calculateP(int num1, int num2) {
        int count = 0, newNum = 0;
        while(num1 != 0) {
            if(num1 % 10== num2)
                count++;
            num1 = num1 / 10;
        }
        if(count > 0) {
            for(inti = 0; i < count; i++) {
                newNum = newNum * 10+ num2;
            }
        }
        return newNum;
    }
}

发表于 2018-05-21 20:48:03 回复(0)
啥头像
总体思路:
  1. 写一个提取函数。     如下代码:
  2. 提取两对数,相加。
代码如下:
#include <iostream>

using namespace std;

int extractNum(int A, int DA);

int main()
{
    int A, DA, B, DB;
    cin >> A >> DA >> B >> DB;
    int PA, PB;
    PA = extractNum(A, DA);
    PB = extractNum(B, DB);
    cout << PA + PB << endl;
    return 0;
}

int extractNum(int A, int DA)
{
    int res = 0;
    while(A>0) {
        int temp = A%10;
        if(temp == DA) {
            res = res*10 + DA;
        }
        A /= 10;
    }
    return res;
} 


发表于 2015-10-14 19:11:30 回复(0)
#include<iostream>
using namespace std;
int main()
{
	string a,b;
	int da,db;
	cin>>a>>da>>b>>db;
	int pa=0,pb=0;
	for(int i=0;i<a.length();i++){
		if(a[i]-'0'==da){
			pa=pa*10+da;
		}
	}
	for(int i=0;i<b.length();i++){
		if(b[i]-'0'==db){
			pb=pb*10+db;
		}
	}
	cout<<(pa+pb);
	return 0;
}

发表于 2020-05-16 10:49:51 回复(0)
include <iostream>
#include <cstring>
using namespace std;
struct STR{
//    string A;
    char A[11]; 
    char B;
}s1,s2;
int CMP(char *a,char b)
{
    int n,i,c=0;
    n=strlen(a);
    for(i=0;i<n;i++){
        if(a[i]==b)
            c++;
    }
    return c;
}
int NUM(char a,int b)
{
    int n,s;
    n=(int)(a-'0');
    s=n;
    if(b==0) return 0;
    else if(b==1) return n;
    else
    for(int j=2;j<=b;j++)
        s=s*10+n;
    return s;
 } 
int main(){
    cin>>s1.A>>s1.B>>s2.A>>s2.B;
    cout<<NUM(s1.B,CMP(s1.A,s1.B))+NUM(s2.B,CMP(s2.A,s2.B));
    return 0;
}
发表于 2020-01-15 19:35:38 回复(0)
#include<cstring>
#include<iostream>
#include<stdlib.h>
#include<cmath>
using namespace std;
int main()
{
    string A, B;
    char dA, dB;
    cin >> A >> dA >> B >> dB;
    string::iterator ia, ib;
    ia = A.begin();
    ib = B.begin();
    int pA = 0;
    int pB = 0;
    int count[2] = { 0 };
    for ( ia = A.begin(); ia!=A.end(); ia++)
    {
        if (*ia == dA) {
            count[0]++;
        }
    }
    for (ib = B.begin(); ib != B.end(); ib++)
    {
        if (*ib == dB) {
            count[1]++;
        }
    }
    for (int i = 0; i < count[0]; i++)
    {
        pA = pA + (dA - '0')*pow(10,i);
    }
    for (int j = 0; j < count[1]; j++)
    {
        pB = pB + (dB - '0')*pow(10,j);
    }
    cout << pA + pB;
    system("PAUSE");
    
}

发表于 2018-10-17 19:53:59 回复(0)
#include <iostream>
#include <cmath>
using namespace std;

int main()

{

    string A, B;

    char DA, DB;

    int sum_A = 0, sum_B = 0;

    int count_A = 0, count_B = 0;


    cin >> A;

    cin >> DA;

    cin >> B;

    cin >> DB;


    for(int i = 0; A[i] != '\0'; i++)

    {

        if(A[i] == DA)

        {

            sum_A += pow(10, count_A) * (A[i] - '0');

            count_A++;

        }

    }

    for(int i = 0; B[i] != '\0'; i++)

    {

        if(B[i] == DB)

        {

            sum_B += pow(10, count_B) * (B[i] - '0');

            count_B++;

        }

    }

    cout << sum_A + sum_B;

}


发表于 2018-04-15 16:29:05 回复(0)
刚开始以为有大位数相加这个操作,结果敲过代码,在看看大家结果,我才知道我想多了。
发表于 2018-02-27 17:11:08 回复(0)
//其实测试用例并不完整,感觉他没考虑Pa和Pb超出int范围的情况,
//如下代码我凭着试一试的心态竟然通过了:

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main(){  string A = "", B = "", Pa = "", Pb = "";  int Da = 0, Db = 0;  int lengthA = 0, lengthB = 0;  int i = 0;    cin >> A >> Da >> B >> Db;  lengthA = A.length();  lengthB = B.length();    //求Pa  for(i = 0;i < lengthA;i++){  if((A[i]-'0')==Da)  Pa += A[i];  }  //求Pb  for(i = 0;i < lengthB;i++){  if((B[i]-'0')==Db)  Pb += B[i];  }    int PaInt = 0, PbInt = 0;  stringstream ss1, ss2;  ss1 << Pa;  ss1 >> PaInt;    ss2 << Pb;  ss2 >> PbInt;  cout<< PaInt+PbInt <<endl;//这里直接相加了,更严谨应该用类似大整数的方法去做。    return 0;
} 

编辑于 2017-12-11 12:12:03 回复(1)
核心代码一行,js的思路
用正则将数据A中的非Da替换掉,然后数字化。同样处理B之后,相加就可以了
var readline = require('readline');
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
    terminal: false
});
rl.on('line', function(line) {
    var arr = line.split(' ');
    console.log((+arr[0].replace(new RegExp("[^" + arr[1] + "]", 'g'), '')) + (+arr[2].replace(new RegExp("[^" + arr[3] + "]", 'g'), '')));
});

编辑于 2017-09-14 17:32:52 回复(1)
#!/usr/bin/env python
#-*- coding:utf8 -*-
def findNum(s1, a, s2, b):
    n1, n2 = 0, 0
    for i in s1:
        if i == a:
            n1 = n1 *10 + 1

    for i in s2:
        if i == b:
            n2 = n2 *10 + 1

    n1 *= int(a) 
    n2 *= int(b)
    return n1+n2

if __name__ == '__main__':
    s1, a, s2, b = raw_input().split()
    print findNum(s1, a, s2, b)


发表于 2017-08-07 11:51:55 回复(0)

主要错在最后那里
int a2=str1.length()>0?Integer.valueOf(str1):0;
要进行判断,要是str1是空的话,用不了这个方法

import java.util.Scanner;
public class Main {

    public static void main(String[] s){
        Scanner sc=new Scanner(System.in);
        while(sc.hasNext()){
            int  a=sc.nextInt();
           int a1=sc.nextInt();
            int b=sc.nextInt();
            int b1=sc.nextInt();

            String str1="";
            String str2="";
          while(a>0){
                if(a%10==a1){
                    str1+=a1;
                }
               a/=10;
            }
           while(b>0){
                if(b%10==b1){
                    str2+=b1;
                }
               b/=10;
            }

            int a2=str1.length()>0?Integer.valueOf(str1):0;
            int b2=str2.length()>0?Integer.valueOf(str2):0;
            System.out.println(a2+b2);
        }
    }
}
发表于 2017-06-02 19:19:08 回复(0)

tmp = raw_input().split(' ')
a = str(tmp[0])
da = tmp[1]
b = str(tmp[2])
db = tmp[3]
def get_result(n,da,a):
for v in range(a.count(da)):
n+=10**v*int(da)
return n
r = get_result(0,da,a)+get_result(0,db,b)
print r
发表于 2017-04-08 00:06:49 回复(0)
解题思路:遍历字符串并转化成数字进行比较即可
importjava.util.Scanner;
 
publicclassMain
{
    privatestaticScanner in =newScanner(System.in);
 
    publicstaticvoidmain(String[] args)
    {
        Integer A = in.nextInt();
        intpa = in.nextInt();
        Integer B = in.nextInt();
        intpb = in.nextInt();
        String strA = A.toString();
        String strB = B.toString();
        inttempA = result(strA, pa);
        inttempB = result(strB, pb);
        in.close();
        System.out.println(tempA + tempB);
    }
 
    publicstaticintresult(String str,intp)
    {
        intres =0;
        for(inti =0; i < str.length(); i++)
        {
            inttemp = str.charAt(i) -'0';
            if(temp == p)
            {
                res = res *10+ p;
            }
        }
        returnres;
    }
}
发表于 2017-04-07 20:08:59 回复(0)
public class Main{
	public static void main(String[] args){
		Scanner s  = new Scanner (System.in);
		int A = s.nextInt();
		int DA = s.nextInt();
		int B = s.nextInt();
		int DB = s.nextInt();
		int sum = print(A,DA)+print(B,DB);
		System.out.println(sum);
	}
	public static int print(int A, int DA){
		//ArrayList<Integer> ls = new ArrayList<>();
		int sum =0;
		String str = String.valueOf(A);
		for(int i=0; i<str.length(); ++i){
			if((str.charAt(i)-'0') == DA){
				sum=DA+sum*10;
			}
		}		
		//int num  = Integer.parseInt(str1);
		return sum;
	}
}

发表于 2017-03-21 16:08:23 回复(0)

#include<iostream>
 
using namespace std;
 
intmain()
{
    intDA,DB;
    longintA,B;
    longintPA=0,PB=0;
    inti,PAnum=0,PBnum=0;
    cin>>A>>DA>>B>>DB;
    while(A>0){
        if(A%10== DA)
            PAnum++;
        A = A/10;
    }
    for(i=0;i<PAnum;i++){
        PA = PA*10+DA;
    }
 
    while(B>0){
        if(B%10== DB)
            PBnum++;
        B = B/10;
    }
    for(i=0;i<PBnum;i++){
        PB = PB*10+DB;
    }
    cout<<PA+PB<<endl;
}

发表于 2017-01-15 16:57:08 回复(0)
import java.util.Scanner;

public class Main{
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String s = in.nextLine();
		in.close();
		String[] str = s.split(" ");
		int a = 0, b = 0;
		Long PA = Long.parseLong(str[1]);
		Long PB = Long.parseLong(str[3]);
		Long sumA =  0L, sumB = 0L;
		for (int i = 0; i < str[0].length(); i++) {
			if (str[1].charAt(0) == str[0].charAt(i)) {
				sumA = (long) (sumA + PA * Math.pow(10, a));
				a++;
			}
		}
		for (int i = 0; i < str[2].length(); i++) {
			if (str[3].charAt(0) == str[2].charAt(i)) {
				sumB = (long) (sumB + PB * Math.pow(10, b));
				b++;
			}
		}
		System.out.println(sumA+sumB);
	}
}

编辑于 2015-08-19 12:36:44 回复(1)
import java.util.Scanner;
public class Main{
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String A = in.next();
		String DA = in.next();
		String B = in.next();
		String DB = in.next();
		StringBuilder sb1 = new StringBuilder();
		StringBuilder sb2 = new StringBuilder();
		for(int i = 0;i<A.length();i++){
			if(A.charAt(i)==DA.charAt(0))
				sb1.append(DA);
		}
		for(int i = 0;i<B.length();i++){
			if(B.charAt(i)==DB.charAt(0))
				sb2.append(DB);
		}
		long a = sb1.length()==0?0:Long.parseLong(sb1.toString());
		long b = sb2.length()==0?0:Long.parseLong(sb2.toString());
		System.out.println(a+b);
		in.close();
	}
}

发表于 2016-06-07 21:34:25 回复(0)
  #include<stdio.h> 
  #include<string.h> 
  long fun(char *str,int d,int len) 
  { 
  int i; 
  int result=0; 
  for(i=0;i<len;i++) 
  { 
  if(str[i]==d+'0') 
  { 
  result=d+result*10; 
  } 
  } 
  return result; 
  } 
  int main() 
  { 
  char str1[11],str2[11]; 
  int da,db; 
  long pa,pb; 
  scanf("%s %d %s %d",str1,&da,str2,&db); 
  int len1=strlen(str1); 
  int len2=strlen(str2); 
  pa=fun(str1,da,len1); 
  pb=fun(str2,db,len2); 
  printf("%d",pa+pb); 
  

  
  
  
  return 0; 
  } 
编辑于 2016-02-03 09:54:29 回复(0)