[NOIP2021] 报数
思路(70Pts):
对于每个数,按照顺序对于每个比它大的数判断是否合法
如何判断一个数是否合法:
- 按位判断是否有
- 对于每个因数按位判断是否有
Coding:
#include <bits/stdc++.h>
using namespace std;
int t;
int x;
bool c(int b){
bool f=true;
while(b){
if(b%10==7) //判断一个数中是否包含7
f=false;
b/=10;
}
return f;
}
bool check(int a){
int b=a;
bool f=true;
for(int i=1;i*i<=a;i++){ //枚举因数
if(a%i==0)
if(c(i)==false||c(a/i)==false) //判断因数是否有7
return false;
}
return true;
}
int find(int n){
for(int i=n; ;i++)
if(check(i)==true) //找到第一个>=n且合法的数
return i;
}
int main( ){
cin>>t;
while(t--){
cin>>x;
if(check(x)==false) //如果上一个数不合法
cout<<"-1"<<endl;
else
cout<<find(x+1)<<endl;
}
return 0;
}