PAT (Basic Level) 1016 部分A+B

C语言:

#include 

long func(long A, int DA) {
    long PA;
    for (PA = 0; A != 0; A /= 10) {
        if (A % 10 == DA) {
            PA = PA * 10 + DA;
        }
    }
    return PA;
}

int main() {
    long A, B;
    int DA, DB;
    scanf("%ld %d %ld %d", &A, &DA, &B, &DB);
    printf("%ld", func(A, DA) + func(B, DB));
    return 0;
}

Java语言(8分,部分超时):

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[] strs = br.readLine().split(" ");
        long A = Long.parseLong(strs[0]);
        int DA = Integer.parseInt(strs[1]);
        long B = Long.parseLong(strs[2]);
        int DB = Integer.parseInt(strs[3]);
        System.out.print(func(A, DA) + func(B, DB));
    }

    public static long func(long A, int DA) {
        long PA;
        for (PA = 0; A != 0; A /= 10) {
            if (A % 10 == DA) {
                PA = PA * 10 + DA;
            }
        }
        return PA;
    }
}

你可能感兴趣的:(PAT (Basic Level) 1016 部分A+B)