B. Fair Numbers(暴力)

B. Fair Numbers

題目

B. Fair Numbers
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
We call a positive integer number fair if it is divisible by each of its nonzero digits. For example, 102102 is fair (because it is divisible by 11 and 22), but 282282 is not, because it isn’t divisible by 88. Given a positive integer 𝑛n. Find the minimum integer 𝑥x, such that 𝑛≤𝑥n≤x and 𝑥x is fair.




Input
The first line contains number of test cases 𝑡t (1≤𝑡≤1031≤t≤103). Each of the next 𝑡t lines contains an integer 𝑛n (1≤𝑛≤10181≤n≤1018).

Output
For each of 𝑡t test cases print a single integer — the least fair number, which is not less than 𝑛n.

Example
inputCopy
4
1
282
1234567890
1000000000000000000
outputCopy
1
288
1234568040
1000000000000000000
Note
Explanations for some test cases:












In the first test case number 11 is fair itself.
In the second test case number 288288 is fair (it’s divisible by both 22 and 88). None of the numbers from [282,287][282,287] is fair, because, for example, none of them is divisible by 88.

暴力做就行,加1判斷,直到出現滿足條件的數

AC代碼

#include <bits/stdc++.h>
#define ll long long
using namespace std;

int main()
{
   
   
ios::sync_with_stdio(false);
    int t;
    cin>>t;
    while(t--)
    {
   
   
        ll n,m;
        cin>>n;
        m=n;
        int flag=0;
        while(!flag)
        {
   
   
            while(n)
            {
   
   
                int x=n%10;
                if(x!=0)
                {
   
   
                    if(m%x!=0)
                    {
   
   
                        m++;
                        n=m;
                        break;
                    }
                }
                n/=10;
            }
            if(n==0)flag++;
        }
        cout<<m<<endl;
    }
    return 0;
}

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