1016 部分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<109。
输出格式:
在一行中输出 PA+PB 的值。
输入样例 1:
3862767 6 13530293 3
输出样例 1:
399
输入样例 2:
3862767 1 13530293 8
输出样例 2:
0
我的思路:
用字符串存数字,然后判断字符,输出。
代码:
package PTA;
import java.io.BufferedInputStream;
import java.util.Scanner;
/**
* Copyright (C), 2019-2021, Kkoo
* Author: Kkoo
* Date: 2021/11/26 0026 0:17
* FileName: PTA_1016
*/
public class PTA_1016 {
public static void main(String[] args) {
Scanner in = new Scanner(new BufferedInputStream(System.in));
//用字符数组存入A B 用字符存入Da Db
char[] a = in.next().toCharArray();
char Da = in.next().charAt(0);
char[] b = in.next().toCharArray();
char Db = in.next().charAt(0);
//用字符串存储Pa Pb
String Pa = "0";
String Pb = "0";
//遍历字符串数组寻找和Da相同的字符 存入Pa
for (char t : a){
if (t==Da){
Pa+=t;
}
}
for (char t : b){
if (t==Db){
Pb+=t;
}
}
//将Pa Pb转为int类型相加输出
System.out.println(Integer.parseInt(Pa)+Integer.parseInt(Pb));
}
}
Comments | NOTHING