本文最后更新于 748 天前,其中的信息可能已经有所发展或是发生改变。
Original Link
思想:
- 贪心,枚举。
- 对于满足条件最大的数,我们枚举其因子 i:
- 保证 i 从 n 开始递减枚举;
- 得到 st=i×i,判断 st 是否可由删除 n 的某些位得到。
- 若首次找到符合条件的数,即为所求;
- 否则,直到 i=1 还未找到满足条件的数,说明不存在。
代码:
| #include <bits/stdc++.h> |
| using namespace std; |
| |
| typedef long long LL; |
| |
| string s; |
| |
| bool check(LL x){ |
| string st = to_string(x); |
| int idx = 0; |
| for(int i = 0; i < s.size(); i ++){ |
| if(idx < st.size() && st[idx] == s[i]) idx ++; |
| } |
| if(idx == st.size()) return 1; |
| return 0; |
| } |
| |
| void solve(){ |
| LL n; cin >> n; |
| s = to_string(n); |
| for(LL i = sqrtl(n); i >= 1; i --){ |
| if(check(i * i)){ |
| string st = to_string(i * i); |
| cout << s.size() - st.size() << endl; |
| return ; |
| } |
| } |
| cout << -1 << endl; |
| } |
| |
| int main(){ |
| int _; cin >> _; |
| while(_ --) solve(); |
| return 0; |
| } |