📜  对于给定的播放速度,在n秒后计算a,b和c

📅  最后修改于: 2021-04-22 06:23:59             🧑  作者: Mango

给定三个不同的字母“ a”“ b”“ c” ,并遵循一定的规则,即每隔2秒,每个“ a”变为一个“ b” ,每隔5秒,每个“ b”变为一个“ c”,并且每隔12秒,每个“ c”会再次变为两个“ a”
从一个“ a”开始,任务是在给定的n秒后找到abc的最终计数。
例子:

方法:可以观察到,a,b和c的值将在每60秒(这是2、5和12的LCM)之后形成一个模式,如下所示:

  • 在n = 60-> a = 32 1时,b = 0,c = 0
  • 在n = 120-> a = 32 2时,b = 0,c = 0
  • 在n = 180-> a = 32 3时,b = 0,c = 0依此类推。

如果n60的倍数,则根据上述观察结果计算结果,否则为最接近n60的倍数计算结果,例如x ,然后将秒数的结果从x +1更新为n
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
#define ull unsigned long long
 
// Function to print the count of
// a, b and c after n seconds
void findCount(int n)
{
    ull a = 1, b = 0, c = 0;
 
    // Number of multiples of 60 below n
    int x = n / 60;
    a = (ull)pow(32, x);
 
    // Multiple of 60 nearest to n
    x = 60 * x;
 
    for (int i = x + 1; i <= n; i++) {
 
        // Change all a to b
        if (i % 2 == 0) {
            b += a;
            a = 0;
        }
 
        // Change all b to c
        if (i % 5 == 0) {
            c += b;
            b = 0;
        }
 
        // Change each c to two a
        if (i % 12 == 0) {
            a += (2 * c);
            c = 0;
        }
    }
 
    // Print the updated values of a, b and c
    cout << "a = " << a << ", ";
    cout << "b = " << b << ", ";
    cout << "c = " << c;
}
 
// Driver code
int main()
{
    int n = 72;
    findCount(n);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
// Function to print the count of
// a, b and c after n seconds
static void findCount(int n)
{
    long a = 1, b = 0, c = 0;
 
    // Number of multiples of 60 below n
    int x = n / 60;
    a = (long)Math.pow(32, x);
 
    // Multiple of 60 nearest to n
    x = 60 * x;
 
    for (int i = x + 1; i <= n; i++)
    {
 
        // Change all a to b
        if (i % 2 == 0)
        {
            b += a;
            a = 0;
        }
 
        // Change all b to c
        if (i % 5 == 0)
        {
            c += b;
            b = 0;
        }
 
        // Change each c to two a
        if (i % 12 == 0)
        {
            a += (2 * c);
            c = 0;
        }
    }
 
    // Print the updated values of a, b and c
    System.out.println("a = " + a + ", b = " +
                           b + ", c = " + c);
}
 
// Driver code
public static void main (String[] args)
{
    int n = 72;
    findCount(n);
}
}
 
// This code is contributed by mits


Python3
# Python3 implementation of the approach
 
# Function to print the count of
# a, b and c after n seconds
import math
def findCount(n):
    a, b, c = 1, 0, 0;
 
    # Number of multiples of 60 below n
    x = (int)(n / 60);
    a = int(math.pow(32, x));
 
    # Multiple of 60 nearest to n
    x = 60 * x;
 
    for i in range(x + 1, n + 1):
 
        # Change all a to b
        if (i % 2 == 0):
            b += a;
            a = 0;
 
        # Change all b to c
        if (i % 5 == 0):
            c += b;
            b = 0;
 
        # Change each c to two a
        if (i % 12 == 0):
            a += (2 * c);
            c = 0;
 
    # Print the updated values of a, b and c
    print("a =", a, end = ", ");
    print("b =", b, end = ", ");
    print("c =", c);
     
# Driver code
if __name__ == '__main__':
    n = 72;
    findCount(n);
 
# This code is contributed
# by 29AjayKumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to print the count of
// a, b and c after n seconds
static void findCount(int n)
{
    long a = 1, b = 0, c = 0;
 
    // Number of multiples of 60 below n
    int x = n / 60;
    a = (long)Math.Pow(32, x);
 
    // Multiple of 60 nearest to n
    x = 60 * x;
 
    for (int i = x + 1; i <= n; i++)
    {
 
        // Change all a to b
        if (i % 2 == 0)
        {
            b += a;
            a = 0;
        }
 
        // Change all b to c
        if (i % 5 == 0)
        {
            c += b;
            b = 0;
        }
 
        // Change each c to two a
        if (i % 12 == 0)
        {
            a += (2 * c);
            c = 0;
        }
    }
 
    // Print the updated values of a, b and c
    Console.WriteLine("a = " + a + ", b = " + b + ", c = " + c);
}
 
// Driver code
static void Main()
{
    int n = 72;
    findCount(n);
}
}
 
// This code is contributed by mits


PHP


Javascript


输出:
a = 64, b = 0, c = 0