美文网首页
PAT-B 1016. 部分A+B (15)

PAT-B 1016. 部分A+B (15)

作者: Rush的博客 | 来源:发表于2016-09-19 09:14 被阅读1248次

    传送门

    https://www.patest.cn/contests/pat-b-practise/1016

    题目

    正整数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
    输出样例1:
    399
    输入样例2:
    3862767 1 13530293 8
    输出样例2:
    0

    分析

    思路就是分别记录A中DA出现的次数和B中DB出现的次数,然后根据其次数,得出实际的PA和PB,最终相加即可。

    源代码

    //C/C++实现
    #include <stdio.h>
    #include <iostream>
    #include <string.h>
    #include <math.h>
    
    using namespace std;
    
    int main(){
        char a[11], b[11];
        char da, db;
        int count1 = 0, count2 = 0;
        int sum1 = 0, sum2 = 0;
        scanf("%s %c %s %c", a, &da, b, &db);
        for(int i = 0; i < strlen(a); i++){
            if(a[i] == da){
                count1++;
            }
        }
        for(int j = 0; j < count1; j++){
            sum1 += (int)(da - '0') * pow((double)10, j);
        }
        for(int i = 0; i < strlen(b); i++){
            if(b[i] == db){
                count2++;
            }
        }
        for(int j = 0; j < count2; j++){
            sum2 += (int)(db - '0') * pow((double)10, j);
        }
        printf("%d\n", sum1 + sum2);
        return 0;
    }
    
    //Java实现
    import java.util.Scanner;
    public class Main {
    
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            long a = scanner.nextLong();
            int da = scanner.nextInt();
            long b = scanner.nextLong();
            int db = scanner.nextInt();
            if(a > 0 && a < 10e10 && b > 0 && b < 10e10 && da >= 0 && da <10 && db >=0 && db < 10) {
                String sa = String.valueOf(a);
                char[] ca = sa.toCharArray();
                char cda = (char)(da+48);
                String sb = String.valueOf(b);
                char[] cb = sb.toCharArray();
                char cdb = (char)(db+48);
                int pa = 0 , pb = 0 , count_da = 0 , count_db = 0;
                for(int i=0;i<sa.length();i++)
                {
                    if(ca[i] == cda)
                    {
                        count_da++;
                    }
                }
                for(int j=0;j<sb.length();j++){
                    if(cb[j] == cdb)
                    {
                        count_db++;
                    }
                }
                for(int k=0;k<count_da;k++){
                    pa += (da * Math.pow(10,k));
                }
                for(int l=0;l<count_db;l++){
                    pb += (db * Math.pow(10,l));
                }
                System.out.println(pa + pb);
            }
        }
    }
    

    相关文章

      网友评论

          本文标题:PAT-B 1016. 部分A+B (15)

          本文链接:https://www.haomeiwen.com/subject/njngettx.html