Problem F: 深入浅出学算法007-统计求和

Description

求含有数字a且不能被a整除的4位整数的个数,并求这些整数的和

Input

多组测试数据,先输入整数T表示组数然后每组输入1个整数a(1<=a<=9)

Output

对于每组测试数据输出一行,每行2个数分别是个数与和

Sample Input

1
3

Sample Output

2112 10568016
#include <stdio.h>
int search(int x,int y)
{
    int a,b,count=0;
    b=x;
    while(x!=0)
    {
        a=x%10;
        x=x/10;
        if(a==y&&b%y!=0)
        {
            count++;
            break;
        }
    }
    if(count==0)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}
int main()
{
    long long int sum;
    int t,a;
    int i,count;
    while(scanf("%d",&t)!=EOF)
    {
        while(t--)
        {
            count=sum=0;
            scanf("%d",&a);
            for(i=1000;i<10000;i++)
            {
                if(search(i,a))
                {
                    sum+=i;
                    count++;
                }
            }
            printf("%d %lld\n",count,sum);
        }
    }
    return 0;
}

 

posted @ 2018-12-24 16:55  MichaelCecil  阅读(1243)  评论(0编辑  收藏  举报