为此,我们将获得棋盘格。我们的任务是使用给定的网格值并使用该值计算平方数。
#include <iostream>
using namespace std;
//求平方数
long int countSquares(int n) {
return (n * (n + 1) / 2) * (2*n + 1) / 3;
}
int main() {
int n = 4;
cout << "Count of squares: " << countSquares(n);
return 0;
}
输出结果
Count of squares: 30