📜  N次移动后的三角形数量

📅  最后修改于: 2021-04-27 20:10:30             🧑  作者: Mango

在第N步中找到三角形的数量,
规则:在起点处画一个等边三角形。在第i步中,取未着色的三角形,将每个三角形均等地划分为4个部分,并对中间部分进行着色。保持三角形计数直到第N步。

例子:

Input : 1
Output : 5 
Explanation: In 1st move we get

Input : 2
Output : 17 
Explanation: In 2nd move we get

天真的方法:
第n个图形中的三角形数目是第(n-1)个图形中的三角形数目+ 2的3倍。通过观察我们可以看到,第n个图形是通过放置与(n-1)个图形相似的3个三角形和一个倒三角形而制成的。我们还考虑了已经形成的更大的三角形。因此,第n个图形中的三角形数量变为(第(n-1)个图形中的三角形数量)* 3 + 2。

C++
// C++ program to calculate the number of equilateral
// triangles
#include 
using namespace std;
// function to calculate number of triangles in Nth step
int numberOfTriangles(int n)
{
    int answer[n + 1] = { 0 };
    answer[0] = 1;
    for (int i = 1; i <= n; i++)
        answer[i] = answer[i - 1] * 3 + 2;
     
    return answer[n];
}
 
// driver program
int main()
{
    int n = 2;
    cout << numberOfTriangles(n);
    return 0;
}


Java
// Java program to find middle of three
// distinct numbers to calculate the
// number of equilateral triangles
import java.util.*;
 
class Triangle
{  
    // function to calculate number of
    // traingles in Nth step
    public static int numberOfTriangles(int n)
    {
        int[] answer = new int[n+1];
        answer[0] = 1;
         
        for (int i = 1; i <= n; i++)
            answer[i] = answer[i - 1] * 3 + 2;
     
        return answer[n];
    }
     
    // driver code
    public static void main(String[] args)
    {
        int n = 2;
        System.out.println(numberOfTriangles(n));
    }
}
 
// This code is contributed by rishabh_jain


Python3
# Python3 code to calculate the
# number of equilateral triangles
 
# function to calculate number
# of traingles in Nth step
def numberOfTriangles (n) :
    answer = [None] * (n + 1);
    answer[0] = 1;
    i = 1
    while i <= n:
        answer[i] = answer[i - 1] * 3 + 2;
        i = i + 1
     
    return answer[n];
 
# Driver code
n = 2
print(numberOfTriangles(n))
 
# This code is contributed by "rishabh_jain".


C#
// C# program to find middle of three
// distinct numbers to calculate the
// number of equilateral triangles
using System;
 
class Triangle
{
    // function to calculate number of
    // traingles in Nth step
    public static int numberOfTriangles(int n)
    {
        int[] answer = new int[n+1];
        answer[0] = 1;
         
        for (int i = 1; i <= n; i++)
            answer[i] = answer[i - 1] * 3 + 2;
     
        return answer[n];
    }
     
    // Driver code
    public static void Main()
    {
        int n = 2;
        Console.WriteLine(numberOfTriangles(n));
    }
}
 
// This code is contributed by vt_m


PHP


Javascript


C++
// C++ program to calculate the number of
// equilateral triangles
#include 
using namespace std;
 
// function to calculate number of triangles
// in Nth step
int numberOfTriangles(int n)
{
    int ans = 2 * (pow(3, n)) - 1;
    return ans;
}
 
// driver program
int main()
{
    int n = 2;
    cout << numberOfTriangles(n);
    return 0;
}


Java
// Java program to find middle of three
// distinct numbers to calculate the
// number of equilateral triangles
import java.util.*;
import static java.lang.Math.pow;
 
class Triangle
{  
    // function to calculate number
    // of triangles in Nth step
    public static double numberOfTriangles(int n)
    {
        double ans = 2 * (pow(3, n)) - 1;
        return ans;
    }
     
    // driver code
    public static void main(String[] args)
    {
        int n = 2;
        System.out.println(numberOfTriangles(n));
    }
}
 
// This code is contributed by rishabh_jain


Python3
# Python3 code to calculate the
# number of equilateral triangles
 
# function to calculate number
# of traingles in Nth step
def numberOfTriangles (n) :
    ans = 2 * (pow(3, n)) - 1;
    return ans;
 
# Driver code
n = 2
print (numberOfTriangles(n))
 
# This code is contributed by "rishabh_jain".


C#
//C# program to find middle of three
// distinct numbers to calculate the
// number of equilateral triangles
using System;
 
class Triangle
{
    // function to calculate number
    // of triangles in Nth step
    public static double numberOfTriangles(int n)
    {
        double ans = 2 * (Math.Pow(3, n)) - 1;
        return ans;
    }
     
    // Driver code
    public static void Main()
    {
        int n = 2;
        Console.WriteLine(numberOfTriangles(n));
    }
}
 
// This code is contributed by vt_m


PHP


Javascript


输出:

17

时间复杂度: O(n)

一个有效的解决方案是为第N个步骤导出公式:
如果我们对每一步都遵循朴素的方法,那么在第N步,我们将得到三角形的数量为

C++

// C++ program to calculate the number of
// equilateral triangles
#include 
using namespace std;
 
// function to calculate number of triangles
// in Nth step
int numberOfTriangles(int n)
{
    int ans = 2 * (pow(3, n)) - 1;
    return ans;
}
 
// driver program
int main()
{
    int n = 2;
    cout << numberOfTriangles(n);
    return 0;
}

Java

// Java program to find middle of three
// distinct numbers to calculate the
// number of equilateral triangles
import java.util.*;
import static java.lang.Math.pow;
 
class Triangle
{  
    // function to calculate number
    // of triangles in Nth step
    public static double numberOfTriangles(int n)
    {
        double ans = 2 * (pow(3, n)) - 1;
        return ans;
    }
     
    // driver code
    public static void main(String[] args)
    {
        int n = 2;
        System.out.println(numberOfTriangles(n));
    }
}
 
// This code is contributed by rishabh_jain

Python3

# Python3 code to calculate the
# number of equilateral triangles
 
# function to calculate number
# of traingles in Nth step
def numberOfTriangles (n) :
    ans = 2 * (pow(3, n)) - 1;
    return ans;
 
# Driver code
n = 2
print (numberOfTriangles(n))
 
# This code is contributed by "rishabh_jain".

C#

//C# program to find middle of three
// distinct numbers to calculate the
// number of equilateral triangles
using System;
 
class Triangle
{
    // function to calculate number
    // of triangles in Nth step
    public static double numberOfTriangles(int n)
    {
        double ans = 2 * (Math.Pow(3, n)) - 1;
        return ans;
    }
     
    // Driver code
    public static void Main()
    {
        int n = 2;
        Console.WriteLine(numberOfTriangles(n));
    }
}
 
// This code is contributed by vt_m

的PHP


Java脚本


输出:

17

时间复杂度: O(log n),需要log n才能计算3 ^ n。