Files
python/GESP/Level6/20240901.cpp

17 lines
509 B
C++
Raw Normal View History

2025-08-30 18:35:01 +08:00
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n;
int f[N]; // 状态表示:f[i]表示i个数最少需要多少个平方数
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
f[i] = i; // 初始化,i个数最少需要i个平方数,因为最坏的情况就是每个数都是1
for (int j = 1; j <= sqrt(i); j++) // 枚举完全平方数
f[i] = min(f[i - j * j] + 1, f[i]); // 状态转移方程
}
cout << f[n] << endl;
return 0;
}