给定一个数字N。任务是编写一个程序,在下面的系列中找到第N项:
null
0, 0, 2, 1, 4, 2, 6, 3, 8, 4, 10, 5, 12, 6, 14, 7, 16, 8,…..
例子 :
Input : N = 10Output : 4Input : N = 7Output : 6
仔细观察后,您会发现该系列由两个系列组成:
- 给定序列中奇数位置的项从0开始按递增顺序形成偶数序列。比如,0,2,4,6,。。
- 给定序列中偶数位置的项是使用公式(previousTerm/2)从上一项导出的。也就是说,在偶数职位上的任期是他们之前任期的一半。
现在,我们知道,每个奇数定位项形成一个从0开始的偶数序列,每个偶数定位项是前一个奇数定位项的一半。 因此,首先检查输入数字N是偶数还是奇数。如果它是奇数,则设置N=(N/2)+1(因为有两个系列并行运行),并使用公式2*(N-1)(N-1,因为系列以0开头)。 同样,如果N为偶数,则设置N=N/2,使用前面的公式将答案除以2。 以下是上述方法的实施情况:
C++
// CPP program to find N-th term // in the series #include <iostream> #include <math.h> using namespace std; // Function to find N-th term // in the series void findNthTerm( int n) { // If n is even if (n % 2 == 0) { n = n / 2; n = 2 * (n - 1); cout << n / 2 << endl; } // If n is odd else { n = (n / 2) + 1; n = 2 * (n - 1); cout << n << endl; } } // Driver code int main() { int X = 10; findNthTerm(X); X = 7; findNthTerm(X); return 0; } |
JAVA
// Java program to find N-th term // in the series // Function to find N-th term // in the series class GFG { static void findNthTerm( int n) { // If n is even if (n % 2 == 0 ) { n = n / 2 ; n = 2 * (n - 1 ); System.out.println(n / 2 ); } // If n is odd else { n = (n / 2 ) + 1 ; n = 2 * (n - 1 ); System.out.println(n); } } // Driver code public static void main(String args[]) { int X = 10 ; findNthTerm(X); X = 7 ; findNthTerm(X); } } // This code is contributed by Subhadeep |
Python 3
# Python 3 program to find N-th term # in the series # Function to find N-th term # in the series def findNthTerm(n): # If n is even if (n % 2 = = 0 ): n = n / / 2 n = 2 * (n - 1 ) print ( n / / 2 ) # If n is odd else : n = (n / / 2 ) + 1 n = 2 * (n - 1 ) print (n) # Driver code if __name__ = = "__main__" : X = 10 findNthTerm(X); X = 7 ; findNthTerm(X) |
C#
// C# program to find N-th term // in the series using System; // Function to find N-th term // in the series class GFG { static void findNthTerm( int n) { // If n is even if (n % 2 == 0) { n = n / 2; n = 2 * (n - 1); Console.Write(n / 2); } // If n is odd else { n = (n / 2) + 1; n = 2 * (n - 1); Console.Write(n); } } // Driver code public static void Main() { int X = 10; findNthTerm(X); Console.Write( "" ); X = 7; findNthTerm(X); } } // This code is contributed // by Smitha |
PHP
<?php // PHP program to find N-th // term in the series // Function to find N-th // term in the series function findNthTerm( $n ) { // If $n is even if ( $n % 2 == 0) { $n = $n / 2; $n = 2 * ( $n - 1); echo $n / 2 . "" ; } // If $n is odd else { $n = (int)( $n / 2) + 1; $n = 2 * ( $n - 1); echo $n . "" ; } } // Driver code $X = 10; findNthTerm( $X ); $X = 7; findNthTerm( $X ); // This code is contributed // by ChitraNayal ?> |
Javascript
<script> // JavaScript program to find N-th term // in the series // Function to find N-th term // in the series function findNthTerm(n) { // If n is even if (n % 2 == 0) { n = Math.floor(n / 2); n = 2 * (n - 1); document.write(Math.floor(n / 2) + "<br>" ); } // If n is odd else { n = Math.floor(n / 2) + 1; n = 2 * (n - 1); document.write(n + "<br>" ); } } // Driver code let X = 10; findNthTerm(X); X = 7; findNthTerm(X); // This code is contributed by Surbhi Tyagi </script> |
输出:
46
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END