例如
i=1;
to_string(i)=”1”
https://codeforces.com/contest/1560/problem/D
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll P2LIM = (ll)2e18;
vector<string> ts;
int solve(string s, string t)
{
int tp = 0;
int sp = 0;
int taken = 0;
while (sp < s.length() && tp < t.length())
{
if(s[sp] == t[tp])
{
taken++;
tp++;
}
sp++;
}
return (int)s.length() - taken + (int)t.length() - taken;
}
int main()
{
for (ll p2 = 1; p2 <= P2LIM; p2 *= 2)
ts.push_back(to_string(p2));
int t;
cin >> t;
while (t--)
{
string n;
cin >> n;
int ans = n.length() + 1;
for (auto p2 : ts)
ans = min(ans, solve(n, p2));
cout << ans << '\n';
}
return 0;
}
https://www.acwing.com/activity/content/code/content/1730529/
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int T;
cin >> T;
while (T -- )
{
string str;
cin >> str;
int n = str.size();
int res = 100;
for (int i = 0; i < 1 << n; i ++ )
{
int x = 0;
for (int j = 0; j < n; j ++ )
if (i >> j & 1)
x = x * 10 + str[j] - '0';
int t = sqrt(x);
if (x && t * t == x) res = min(res, n - (int)to_string(x).size());
}
if (res == 100) res = -1;
cout << res << endl;
}
return 0;
}