CF248B题解

· · 题解

前言

我看楼下的大佬用的都是找规律的方法,那本蒟蒻就在这里提出一种新颖的解法。QAQ

题目大意

题目传送门

给定一个数 n,表示 n 位数。你需要在这 n 位数之中找出最小的且能被 2, 3, 5, 7 这四个数整除。

思路

可以枚举 n 位数中的所有的数,寻找是 2, 3, 5, 7 的倍数的数。找到了就return 0结束程序。如果没找到,到循环完后,再输出-1。

代码

#include <iostream>
#include <cmath>
using namespace std;

int n;
int main(){
    cin>>n;
    long long x;//x表示n位数中最大的数
    int x1=n;
    while(x1>0){
        x1--;
        x*=10,x+=9;
    }
    for(int i=pow(10,n-1);i<=x;i++){
        if(i%210==0){//判断是否是210的倍数(即2,3,5,7的倍数)
            cout<<i<<endl;
            return 0;//要记得结束,不然会输出所有的数
        }
    }
    cout<<-1<<endl;
    return 0;
}