📌  相关文章
📜  程序以找到系列a,b,b,c,c,c,……的第N个项。

📅  最后修改于: 2021-05-31 19:25:48             🧑  作者: Mango

给定数字N。任务是编写一个程序来查找以下系列中的第N个术语:

例子

Input : 12
Output : e

Input : 288
Output : x

想法是使用AP和公式来找到该问题的解决方案。显然,该系列被描绘为1a,2b,3c,4d,5e等。因此使其成为AP。
现在我们可以使用AP总和公式:

sum = (n/2)*(a + (n-1)*d)  

在这种情况下,它变为sum =(n(n + 1))/ 2 (由于a = 1且d = 1),其中“ sum”在这里是给定的第N个项。
下面是上述方法的实现:

C++
// CPP program to find nth term of the
// given series
#include 
using namespace std;
 
// Function to find nth term of the
// given series
void findNthTerm(int n)
{
    // Let us find roots of equation x * (x + 1)/2 = n
    n = n * 2;
    int a = 1, b = 1, c = -1 * n;
    int d = b * b - 4 * a * c;
    double sqrt_val = sqrt(abs(d));
    int x1 = (double)(-b + sqrt_val) / (2 * a);
    int x2 = (double)(-b - sqrt_val) / (2 * a);
 
    if (x1 >= 1)
        cout << (char)('a' + x1) << endl;
    else if (x2 >= 1)
        cout << (char)('a' + x2) << endl;
}
 
// Driver program
int main()
{
    int n = 12;
    findNthTerm(n);
 
    n = 288;
    findNthTerm(n);
 
    return 0;
}


Java
// Java program to find nth
// term of the given series
import java.io.*;
 
class GFG {
 
    // Function to find nth term
    // of the given series
    static void findNthTerm(int n)
    {
        // Let us find roots of
        // equation x * (x + 1)/2 = n
        n = n * 2;
        int a = 1, b = 1, c = -1 * n;
        int d = b * b - 4 * a * c;
        double sqrt_val = Math.sqrt(Math.abs(d));
        int x1 = (int)((-b + sqrt_val) / (2 * a));
        int x2 = (int)((-b - sqrt_val) / (2 * a));
 
        if (x1 >= 1)
            System.out.println((char)('a' + x1));
        else if (x2 >= 1)
            System.out.println((char)('a' + x2));
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int n = 12;
        findNthTerm(n);
 
        n = 288;
        findNthTerm(n);
    }
}
 
// This code has been contributed
// by anuj_67.


Python 3
# Python 3 program to find nth
# term of the given series
import math
 
# Function to find nth term
# of the given series
def findNthTerm(n):
 
    # Let us find roots of
    # equation x * (x + 1)/2 = n
    n = n * 2
    a = 1
    b = 1
    c = -1 * n
    d = b * b - 4 * a * c
    sqrt_val = math.sqrt(abs(d))
    x1 = (-b + sqrt_val) // (2 * a)
    x2 = (-b - sqrt_val) // (2 * a)
    x1 = int(x1)
    x2 = int(x2)
     
    # ASCII of 'a' is 97
    if (x1 >= 1):
        print(chr(97+x1))
    elif (x2 >= 1):
        print(chr(97+x2))
 
# Driver Code
if __name__ == "__main__":
    n = 12
    findNthTerm(n)
 
    n = 288
    findNthTerm(n)
 
# This code is contributed
# by ChitraNayal


C#
// C# program to find nth
// term of the given series
using System;
 
public class GFG {
 
    // Function to find nth term
    // of the given series
    static void findNthTerm(int n)
    {
        // Let us find roots of
        // equation x * (x + 1)/2 = n
        n = n * 2;
        int a = 1, b = 1, c = -1 * n;
        int d = b * b - 4 * a * c;
        double sqrt_val = Math.Sqrt(Math.Abs(d));
        int x1 = (int)((-b + sqrt_val) / (2 * a));
        int x2 = (int)((-b - sqrt_val) / (2 * a));
 
        if (x1 >= 1)
            Console.WriteLine((char)('a' + x1));
        else if (x2 >= 1)
            Console.WriteLine((char)('a' + x2));
    }
 
    // Driver Code
    static public void Main(String[] args)
    {
        int n = 12;
        findNthTerm(n);
 
        n = 288;
        findNthTerm(n);
    }
}
// contributed by Arnab Kundu


PHP
= 1)
        echo chr(97+$x1) . "\n";
    else if ((int)$x2 >= 1)
        echo chr(97+$x2), "\n";
}
 
// Driver Code
$n = 12;
findNthTerm($n);
 
$n = 288;
findNthTerm($n);
 
// This Code is contributed by mits
?>


Javascript


输出:
e
x
想要从精选的最佳视频中学习并解决问题,请查看有关从基础到高级C++的C++基础课程以及有关语言和STL的C++ STL课程。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”