Codeup 6170.部分A+B

--------------------
/* 
* Copyright (c) 2014, 烟台大学计算机学院 
* All rights reserved. 
* 文件名称:test.cpp 
* 作    者:曾建强 
* 完成日期:
* 版 本 号:v1.0 

* 问题描述:
* 输入描述:
* 程序输出:
*/ 
--------------------- 

 

题目描述

正整数A的“D A(为1位整数)部分”定义为由A中所有D A组成的新整数P A。例如:给定A = 3862767,D A  = 6,则A的“6部分” P A是66,因为A中有2个6。

现给定A,d ,B,d ,请编写程序计算P   + P 

输入

输入在一行中依次给出A,D ,B,D ,中间以空格分隔,其中0 <A,B <10 10 

输出

在一行中输出P  + P 的值。

样例输入

3862767 6 13530293 3

3862767 1 13530293 8

样例输出

399

0

 

#include <stdio.h>
int main()
{
    int n(int a,int b);
    int num(int c,int d);
    long long A,B;
    int DA,DB,PA=0,PB=0;
    while (scanf("%lld %d %lld %d",&A,&DA,&B,&DB)!=EOF)
    {
        int i,j;
        i=n(A,DA);
        j=n(B,DB);
        PA=num(DA,i);
        PB=num(DB,j);
        printf("%d\n",PA+PB);
    }
    return 0;
}

//求输入的数字中包含几个DA
int n(int a,int b)
{
    int m=0;
    while (a)
    {
        if (a%10==b)
            m++;
        a=a/10;
    }
    return m;
}

//输出PA
int num(int c,int d)
{
    int b=0;
    while (d)
    {
        b=c+b*10;
        d--;
    }
    return b;
}

 

运行截图:

Codeup 6170.部分A+B