1770 数数字

1770 数数字
基准时间限制:1 秒 空间限制:262144 KB 分值: 20 难度:3级算法题

统计一下 aaa  aaana × b 的结果里面有多少个数字da,b,d均为一位数。

样例解释:

3333333333*3=9999999999,里面有109


Input
多组测试数据。
第一行有一个整数T,表示测试数据的数目。(1≤T≤5000)
接下来有T行,每一行表示一组测试数据,有4个整数a,b,d,n。 (1≤a,b≤9,0≤d≤9,1≤n≤10^9)
Output
对于每一组数据,输出一个整数占一行,表示答案。
Input示例
2
3 3 9 10
3 3 0 10
Output示例
10
0

代码:

#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<map>
typedef long long ll;
using namespace std;
const int N=1e6+10;
int main()
{
    int t,a,b,d,n;
    cin>>t;
    while(t--)
    {
        cin>>a>>b>>d>>n;
        if(a*b<10)
        {
            if(d==a*b)
                cout<<n<<endl;
            else
                cout<<0<<endl;
        }
        else
        {
            int r=(a*b)%10;
            int z=(a*b)/10;//表示进位
            int s[10];
            memset(s,0,sizeof(s));
            s[r]++;
            n--;//处理完最右边的一位数字,n减一
            while(n)
            {
                r=(a*b+z)%10;
                z=(a*b+z)/10;//表示最左边的一位数
                if(s[r])
                {
                    s[r]+=n;
                    break;
                }
                else
                    s[r]++;
                n--;
            }
            s[z]++;
            cout<<s[d]<<endl;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_40510246/article/details/79834683