Files
python/GESP/Level6/20240901.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

17 lines
509 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}