点击跳转例题
思路:
我们直接进行模拟即可,判断一个数是不是平方数简单,但是如何判断它拆开的某种方式都是平方数呢?,我们可以枚举每一种拆分方式,用字符串来分割。
代码:
#include <bits/stdc++.h> #define int long long //(有超时风险) #define PII pair<int,int> #define endl '\n' #define LL __int128 using namespace std; //判断一个数是不是平方数 bool check1(string s) { int sum=0; for(int i=0;i<s.size();i++) { sum*=10; sum+=s[i]-'0'; } if(sum==0) return false; int tmp= sqrt(sum); return tmp*tmp==sum; } //分割每种情况来判断。 bool check2(string s) { for(int i=1;i<s.size();i++) { if(check1(s.substr(0,i))&&check1(s.substr(i))) return true; } return false; } signed main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int l,r;cin>>l>>r; for(int i=l;i<=r;i++) { string s=to_string(i); if(check1(s)&&check2(s)) cout<<i<<endl; } return 0; }