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

评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值