📜  查找给定方格内的方格数

📅  最后修改于: 2021-04-23 19:41:03             🧑  作者: Mango

给定N * N边的网格,任务是查找其中存在的正方形总数。所有选择的正方形可以是任意长度。

例子:

方法1:举几个例子,可以观察到,对于大小为N * N的网格,其内部的正方形数将为1 2 + 2 2 + 3 2 +…+ N 2

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Function to return the number
// of squares inside an n*n grid
int cntSquares(int n)
{
    int squares = 0;
    for (int i = 1; i <= n; i++) {
        squares += pow(i, 2);
    }
    return squares;
}
  
// Driver code
int main()
{
    int n = 4;
  
    cout << cntSquares(4);
  
    return 0;
}


Java
// Java implementation of the approach
class GFG {
  
    // Function to return the number
    // of squares inside an n*n grid
    static int cntSquares(int n)
    {
        int squares = 0;
        for (int i = 1; i <= n; i++) {
            squares += Math.pow(i, 2);
        }
        return squares;
    }
  
    // Driver code
    public static void main(String args[])
    {
        int n = 4;
  
        System.out.print(cntSquares(4));
    }
}


Python3
# Python3 implementation of the approach 
  
# Function to return the number 
# of squares inside an n*n grid 
def cntSquares(n) : 
  
    squares = 0; 
    for i in range(1, n + 1) :
        squares += i ** 2; 
  
    return squares; 
  
# Driver code 
if __name__ == "__main__" : 
  
    n = 4; 
  
    print(cntSquares(4)); 
  
# This code is contributed by AnkitRai01


C#
// C# implementation of the approach
using System;
  
class GFG 
{
  
    // Function to return the number
    // of squares inside an n*n grid
    static int cntSquares(int n)
    {
        int squares = 0;
        for (int i = 1; i <= n; i++) 
        {
            squares += (int)Math.Pow(i, 2);
        }
        return squares;
    }
  
    // Driver code
    public static void Main(String []args)
    {
        int n = 4;
  
        Console.Write(cntSquares(n));
    }
}
  
// This code is contributed by 29AjayKumar


C++
// C++ implementation of the approach
#include 
  
using namespace std;
  
int cnt_squares (int n)
{
    /* Function to return the number
     of squares inside an n*n grid */
  
    return n * (n + 1) * (2 * n + 1) / 6;
}
  
// Driver code
int main()
{
    cout << cnt_squares (4) << endl;
  
    return 0;
}


Java
// Java implementation of the approach
class GFG {
    static int cntSquares (int n) {
        /* Function to return the number
        of squares inside an n*n grid */
      
        return n * (n + 1) * (2 * n + 1) / 6;
    }
  
    // Driver code
    public static void main(String args[]) {
        System.out.println (cntSquares(4));
    }
}


Python3
# Python3 implementation of the approach 
  
"""
Function to return the number 
of squares inside an n*n grid 
"""
  
def cntSquares(n) : 
    return int (n * (n + 1) * (2 * n + 1) / 6)
  
# Driver code 
if __name__ == "__main__" : 
    print (cntSquares (4));


C#
// C# implementation of the approach
using System;
  
class GFG 
{
  
    /* Function to return the number
     of squares inside an n*n grid */
    static int cntSquares (int n)
    {
        return n * (n + 1) * (2 * n + 1) / 6;
    }
  
    // Driver code
    public static void Main (String[] args)
    {
        Console.Write (cntSquares (4));
    }
}


输出:
30

方法2:通过使用直接公式。
但是,总和  \displaystyle \sum_{i=1}^{n}k^{2} 具有封闭形式(直接公式)  \displaystyle \frac{n\left(n+1\right)\left(2n+1\right)}{6} 。因此,我们可以使用它来计算\displaystyle O(1) 时间。

下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
  
using namespace std;
  
int cnt_squares (int n)
{
    /* Function to return the number
     of squares inside an n*n grid */
  
    return n * (n + 1) * (2 * n + 1) / 6;
}
  
// Driver code
int main()
{
    cout << cnt_squares (4) << endl;
  
    return 0;
}

Java

// Java implementation of the approach
class GFG {
    static int cntSquares (int n) {
        /* Function to return the number
        of squares inside an n*n grid */
      
        return n * (n + 1) * (2 * n + 1) / 6;
    }
  
    // Driver code
    public static void main(String args[]) {
        System.out.println (cntSquares(4));
    }
}

Python3

# Python3 implementation of the approach 
  
"""
Function to return the number 
of squares inside an n*n grid 
"""
  
def cntSquares(n) : 
    return int (n * (n + 1) * (2 * n + 1) / 6)
  
# Driver code 
if __name__ == "__main__" : 
    print (cntSquares (4)); 

C#

// C# implementation of the approach
using System;
  
class GFG 
{
  
    /* Function to return the number
     of squares inside an n*n grid */
    static int cntSquares (int n)
    {
        return n * (n + 1) * (2 * n + 1) / 6;
    }
  
    // Driver code
    public static void Main (String[] args)
    {
        Console.Write (cntSquares (4));
    }
}
输出:
30