首页 > 试题广场 >

1016. 部分A+B (15)

[编程题]1016. 部分A+B (15)
  • 热度指数:29795 时间限制: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
import java.util.Scanner;

/**
 * @ClassName Main
 * @Description TODO
 * @Author Wlison
 * @Date 2020/3/11 9:38
 * @Version 1.0
 **/
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        while (sc.hasNext()) {
            int a = sc.nextInt();
            int b = sc.nextInt();
            int c = sc.nextInt();
            int d = sc.nextInt();
            String as = "";
            String cs = "";
            while(a!=0){
                if (a%10==b) as+=b;
                a/=10;
            }
            while(c!=0){
                if (c%10==d) cs+=d;
                c/=10;
            }
            a=as.equals("")?0:Integer.parseInt(as);
            c=cs.equals("")?0:Integer.parseInt(cs);
            System.out.println(a+c);
        }
    }
}

发表于 2020-03-11 10:32:12 回复(0)

import java.util.Scanner;

public class Test1006 {

    public static void main(String[] args) {

        Scanner scan = new Scanner(System.in);

        int A,DA,B,DB,PA=0,PB=0;

        A = scan.nextInt();

        DA = scan.nextInt();

        B = scan.nextInt();

        DB = scan.nextInt();

        PA=P(A,DA);

        PB=P(B,DB);

        System.out.println(PA+PB);

    }

    public static int P(int A,int DA) {

        int PA=0,count=1;

        while(A>0) {

            if(A%10==DA) {

                PA+=count*DA;

                count*=10;

            }

            A/=10;

        }

        return PA;

    }

}


发表于 2020-02-05 15:30:43 回复(0)
import java.util.Scanner;

public clas***ain {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt(),
                da = scanner.nextInt(),
                b = scanner.nextInt(),
                db = scanner.nextInt(),
                pa = 0, pb = 0;
        char[] arrayA = String.valueOf(a).toCharArray();
        int flag = 0;
        for (int i = 0; i < arrayA.length; i++) {
            if (arrayA[i] == String.valueOf(da).toCharArray()[0]) {
                pa += Math.pow(10, flag) * da;
                flag++;
            }
        }
        char[] arrayB = String.valueOf(b).toCharArray();
        int flag2 = 0;
        for (int i = 0; i < arrayB.length; i++) {
            if (arrayB[i] == String.valueOf(db).toCharArray()[0]) {
                pb += Math.pow(10, flag2) * db;
                flag2++;
            }
        }
        System.out.println(pa + pb);
    }
}

发表于 2019-05-16 10:56:43 回复(0)
import java.util.Scanner;
public class Main{
 private static int han(String a,int b) {
  int p=0;
  int o=0;
  for(int i=0;i<a.length();i++) {
   if(a.charAt(i)-'0'==b) {
    p++;
   }
  }
  for(int i=0;i<p;i++) {
   o+=b*Math.pow(10, i);
  }
  return o;
 }
 public static void main(String[] args) {
  Scanner in=new Scanner(System.in);
  while(in.hasNext()) {
  String a=in.next();
  int b=in.nextInt();
  String c=in.next();
  int d=in.nextInt();
        System.out.println(han(a,b)+han(c,d));
 }
}
}

发表于 2019-03-08 19:57:18 回复(0)
import java.util.Scanner;

public class Main {

    private static Scanner sc;

    public static void main(String[] args) {
        sc = new Scanner(System.in);
        int A = sc.nextInt();
        int DA = sc.nextInt();
        int PA = 0;
        int B = sc.nextInt();
        int DB = sc.nextInt();
        int PB = 0;
        while(A > 0) {
            if(A % 10 == DA) {
                PA = PA * 10 + DA;
            }
            A /= 10;
        }
        while(B > 0) {
            if(B % 10 == DB) {
                PB = PB * 10 + DB;
            }
            B /= 10;
        }
        System.out.println(PA + PB);
    }
}
编辑于 2018-09-06 11:14:08 回复(0)
import java.util.Scanner;
/*
输入在一行中依次给出A、DA、B、DB,中间以空格分隔,其中0 < A, B < 1010。
输出描述:
在一行中输出PA + PB的值。
输入例子:
3862767 6 13530293 3
输出例子:
399
*/
/*
    author: MR.Yang
    date:2018.05.26
*/
/*
思路:用了一个Math.pow(double a,int b)方法(a的b次幂)求P的值
参数a表示底数,b表示指数
传回的数值是double类型
*/
public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int A = in.nextInt();
        int DA = in.nextInt();
        int B = in.nextInt();
        int DB = in.nextInt();
        Main main = new Main();
        int Pa = main.getP(A,DA);
        int Pb = main.getP(B,DB);
        System.out.println(Pa+Pb);
    }
    public int getP(int A,int B){
        int count = 0;
        int P=0;
        while(A != 0){
            if(A%10 == B){
            count++;
            }
            A = A/10;
        }
        for(int i=0; i<count; i++){
            P = P + ((int)(Math.pow(10,i)))*B;
        }
        return P;
    }
}

发表于 2018-05-26 14:47: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)


//【java】28行若此处写String part ="";case通过率75?因为也可能没匹配到,就是0了
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String []a = new String[4];
for (int i = 0; i < a.length; i++) {
a[i]=scanner.next();
}
scanner.close();
System.out.println(getPart(a[0],a[1])+getPart(a[2],a[3]));
}
public static int getPart(String str1,String str2) {
char []c = str1.toCharArray();
String part = "0";//若此处写="";case通过率75???什么原因
for (int i = 0; i < c.length; i++) {
if (str2.equals(c[i]+"")) {
part=part + str2;
}
}
return Integer.parseInt(part);
}
}


编辑于 2018-04-20 00:11:11 回复(0)
import java.util.Scanner;
public class Main {

    public static void main(String[]args)
    {
        Scanner scan=new Scanner(System.in);
        long  a,b;
        int da,db;
        a=scan.nextInt();
        da=scan.nextInt();
        b=scan.nextInt();
        db=scan.nextInt();
        System.out.println(getNum(a,da)+getNum(b,db));
    }
    public static long  getNum(long  k,int dk)
    {
        int p=0;
        int count=0;
        while(k>0)
        {
            if(k%10==dk)
            {
                count++;
            }
            k=k/10;
        }
        for(int i=0;i<count;i++)
            p=p*10+dk;

        return p;
    }
}

发表于 2018-04-16 23:18:23 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
 
public class Main {
    public static void main(String[] args) throws IOException {
         
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
          
        String line = br.readLine();
        String[] parameters = line.split(" ");
 
        int PA = 0;
        int PB = 0;
         
        int DA = Integer.parseInt(parameters[1]);
        int DB = Integer.parseInt(parameters[3]);
         
        for(int i = 0; i < parameters[0].length(); i++) {
            if(parameters[0].charAt(i) == parameters[1].charAt(0)) {
                PA = PA * 10 + DA;
            }
        }
         
        for(int i = 0; i < parameters[2].length(); i++) {
            if(parameters[2].charAt(i) == parameters[3].charAt(0)) {
                PB = PB * 10 + DB;
            }
        }
         
        System.out.println(PA + PB);
    }
}

编辑于 2017-11-15 14:58:57 回复(0)

import java.util.Scanner;


public class Main {

    String result;

    String A,B,A1,B1;

    char DA,DB;

    public Main() {

        Scanner sc=new Scanner(System.in);

        A=sc.next();

        A1=sc.next();

        B=sc.next();

        B1=sc.next();

        int a=Integer.parseInt(D(A,A1.charAt(0)));

        int b=Integer.parseInt(D(B,B1.charAt(0)));

        System.out.println(a+b);

    }

    public String D(String A,char DA) {

        result="";

        for(int i=0;i<A.length();i++) {

            if(A.charAt(i)==DA)

                result+=DA;

        }

        if(result=="")

            result="0";

        return result;

    }

    public static void main(String[] args) {

        Main p=new Main();

}

}

发表于 2017-10-21 10:45:18 回复(0)
import java.util.Scanner;

public class Main {     public static int method(int A, int Da) {         int Pa = 0;         int i = 0;         while (A > 0) {             if (A % 10 == Da) {                 Pa += Math.pow(10, i) * Da;                 i++;                 A /= 10;             } else {                 A /= 10;             }         }         return Pa;     }     @SuppressWarnings("resource")     public static void main(String[] args) {         Scanner in = new Scanner(System.in);         int A = in.nextInt();         int Da = in.nextInt();         int B = in.nextInt();         int Db = in.nextInt();         System.out.println(method(A, Da) + method(B, Db));     }
}

发表于 2017-09-21 20:02:50 回复(0)
唯一自己通过的一个,纪念下。
发表于 2017-09-09 13:18:40 回复(0)
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		long A = sc.nextLong();
		int DA = sc.nextInt();
		long B = sc.nextLong();
		int DB = sc.nextInt();
		System.out.println(transTo(A, DA)+transTo(B, DB));
	}
	public static int transTo (long a, int b){
		char str [] = new String(a+"").toCharArray();
		String P ="";
		for (char c : str) {
			if(Integer.parseInt(c+"")==b){
				P = P + b;
			}
		}
		if(P.equals("")){
			return 0;
		}else{
			return Integer.parseInt(P);
		}
	}
}

发表于 2017-08-14 10:48:00 回复(0)
import java.util.Scanner;
public class Main{
	@SuppressWarnings("resource")
	public static void main(String[] args) {
		int a=0;
		int b=0;
		int Da = 0;
		int Db=0;
		
		Scanner sc = new Scanner(System.in);
		a = sc.nextInt();
		Da = sc.nextInt();
		b = sc.nextInt();
		Db = sc.nextInt();
		
		int numa = 0;
		String stra = String.valueOf(a);
		for(int i =0;i<stra.length();i++){
			if(stra.charAt(i)==String.valueOf(Da).charAt(0)){
				numa++;
			}
		}
		int numb = 0;
		String strb = String.valueOf(b);
		for(int i =0;i<strb.length();i++){
			if(strb.charAt(i)==String.valueOf(Db).charAt(0)){
				numb++;
			}
		}
		System.out.println(getNum(Da,numa)+getNum(Db,numb));
		
	}
	
	public static int getNum(int n,int num){
		int a=n;
		int b =0;
		for(int i=0;i<num;i++){
			b+=a;
			a=a*10;
		}
		return b;
	}
}
发表于 2017-04-05 14:59:48 回复(0)
import java.util.Scanner;

public class Main {
public static void main(String[] args) {
long A , B;
int D_A ,D_B ;
StringBuffer A_str , B_str;
String P_A = "0" , P_B = "0";
Scanner sc = new Scanner(System.in);
A = sc.nextLong();
D_A = sc.nextInt();
B = sc.nextLong();
D_B = sc.nextInt();
A_str = new StringBuffer(Long.toString(A));
B_str = new StringBuffer(Long.toString(B));
int count_A = 0;
int count_B = 0;
for(int i=0;i<A_str.length();i++){
if(A_str.charAt(i)==(char)(D_A+48)){
count_A ++;
}
}
for(int i=0;i<B_str.length();i++){
if(B_str.charAt(i)==(char)(D_B+48)){
count_B ++;
}
}
for(int i=0;i<count_A;i++){
P_A+=(char)(D_A+48);
}
for(int i=0;i<count_B;i++){
P_B+=(char)(D_B+48);
}
try{
System.out.print(Integer.valueOf(P_A)+Integer.valueOf(P_B));
}
catch (Exception e) {
}
}
}

。。。。写得好乱 好蠢。。不过还是通过了
发表于 2017-01-26 16:59:17 回复(0)