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;
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章