跳到正文

题解

[NOIP2021] 报数

piske约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年8月14日
收录
2025年12月12日
更新
2026年11月11日
篇幅
约 1 分钟

版本

  1. v1当前

[NOIP2021] 报数

思路(70Pts):

对于每个数,按照顺序对于每个比它大的数判断是否合法
如何判断一个数是否合法:

  • 按位判断是否有 77
  • 对于每个因数按位判断是否有 77

Coding:

C++
#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;
}
原文