考虑一个序列a 0 ,a 1 ,…,a n ,其中a i = a i-1 + a i-2 。给定一个0 ,一个1和一个正整数n 。任务是找出n是奇数还是偶数。
请注意,给定的序列类似于Fibonacci,不同之处在于前两个项可以是任意值,而不是0或1。
例子 :
Input : a0 = 2, a1 = 4, n =3
Output : Even
a2 = 6, a3 = 10
And a3 is even.
Input : a0 = 1, a1 = 9, n = 2
Output : Even
方法1:想法是使用数组查找序列,并检查第n个元素是偶数还是奇数。
C++
// CPP Program to check if the nth is odd or even in a
// sequence where each term is sum of previous two term
#include
using namespace std;
#define MAX 100
// Return if the nth term is even or odd.
bool findNature(int a, int b, int n)
{
int seq[MAX] = { 0 };
seq[0] = a;
seq[1] = b;
for (int i = 2; i <= n; i++)
seq[i] = seq[i - 1] + seq[i - 2];
// Return true if odd
return (seq[n] & 1);
}
// Driven Program
int main()
{
int a = 2, b = 4;
int n = 3;
(findNature(a, b, n) ? (cout << "Odd"
<< " ")
: (cout << "Even"
<< " "));
return 0;
}
Java
// Java Program to check if
// the nth is odd or even
// in a sequence where each
// term is sum of previous
// two term
// Return if the nth
// term is even or odd.
class GFG
{
public static int findNature(int a,
int b, int n)
{
int[] seq = new int[100];
seq[0] = a;
seq[1] = b;
for (int i = 2; i <= n; i++)
seq[i] = seq[i - 1] + seq[i - 2];
// Return true if odd
if((seq[n] & 1) != 0)
return 1;
else
return 0;
}
// Driver Code
public static void main(String[] args)
{
int a = 2, b = 4;
int n = 3;
if(findNature(a, b, n) == 1)
System.out.println("Odd ");
else
System.out.println("Even ");
}
}
// This code is contributed
// by mits
Python3
# Python3 Program to check if
# the nth is odd or even in a
# sequence where each term is
# sum of previous two term
MAX = 100;
# Return if the nth
# term is even or odd.
def findNature(a, b, n):
seq = [0] * MAX;
seq[0] = a;
seq[1] = b;
for i in range(2, n + 1):
seq[i] = seq[i - 1] + seq[i - 2];
# Return true if odd
return (seq[n] & 1);
# Driver Code
a = 2;
b = 4;
n = 3;
if(findNature(a, b, n)):
print("Odd");
else:
print("Even");
# This code is contributed by mits
C#
// C# Program to check if the
// nth is odd or even in a
// sequence where each term
// is sum of previous two term
using System;
// Return if the nth term
// is even or odd.
class GFG
{
public static int findNature(int a,
int b, int n)
{
int[] seq = new int[100];
seq[0] = a;
seq[1] = b;
for (int i = 2; i <= n; i++)
seq[i] = seq[i - 1] +
seq[i - 2];
// Return true if odd
if((seq[n] & 1)!=0)
return 1;
else
return 0;
}
// Driver Code
public static void Main()
{
int a = 2, b = 4;
int n = 3;
if(findNature(a, b, n) == 1)
Console.Write("Odd ");
else
Console.Write("Even ");
}
}
// This code is contributed
// by mits
PHP
C++
// CPP Program to check if the nth is odd or even in a
// sequence where each term is sum of previous two term
#include
using namespace std;
// Return if the nth term is even or odd.
bool findNature(int a, int b, int n)
{
if (n == 0)
return (a & 1);
if (n == 1)
return (b & 1);
// If a is even
if (!(a & 1)) {
// If b is even
if (!(b & 1))
return false;
// If b is odd
else
return (n % 3 != 0);
}
// If a is odd
else {
// If b is odd
if (!(b & 1))
return ((n - 1) % 3 != 0);
// If b is eve
else
return ((n + 1) % 3 != 0);
}
}
// Driven Program
int main()
{
int a = 2, b = 4;
int n = 3;
(findNature(a, b, n) ? (cout << "Odd"
<< " ")
: (cout << "Even"
<< " "));
return 0;
}
Java
// Java Program to check if the nth is odd or even in a
// sequence where each term is sum of previous two term
class GFG{
// Return if the nth term is even or odd.
static boolean findNature(int a, int b, int n)
{
if (n == 0)
return (a & 1)==1?true:false;
if (n == 1)
return (b & 1)==1?true:false;
// If a is even
if ((a & 1)==0) {
// If b is even
if ((b & 1)==0)
return false;
// If b is odd
else
return (n % 3 != 0);
}
// If a is odd
else {
// If b is odd
if ((b & 1)==0)
return ((n - 1) % 3 != 0);
// If b is eve
else
return ((n + 1) % 3 != 0);
}
}
// Driven Program
public static void main(String[] args)
{
int a = 2, b = 4;
int n = 3;
if(findNature(a, b, n))
System.out.println("Odd");
else
System.out.println("Even");
}
}
// This Code is contributed by mits
Python3
# Python3 Program to check if the
# nth is odd or even in a
# sequence where each term is
# sum of previous two term
# Return if the nth
# term is even or odd.
def findNature(a, b, n):
if (n == 0):
return (a & 1);
if (n == 1):
return (b & 1);
# If a is even
if ((a & 1) == 0):
# If b is even
if ((b & 1) == 0):
return False;
# If b is odd
else:
return True if(n % 3 != 0) else False;
# If a is odd
else:
# If b is odd
if ((b & 1) == 0):
return True if((n - 1) % 3 != 0) else False;
# If b is eve
else:
return True if((n + 1) % 3 != 0) else False;
# Driver Code
a = 2;
b = 4;
n = 3;
if (findNature(a, b, n) == True):
print("Odd", end = " ");
else:
print("Even", end = " ");
# This code is contributed by mits
C#
// C# Program to check if the nth is odd or even in a
// sequence where each term is sum of previous two term
class GFG{
// Return if the nth term is even or odd.
static bool findNature(int a, int b, int n)
{
if (n == 0)
return (a & 1)==1?true:false;
if (n == 1)
return (b & 1)==1?true:false;
// If a is even
if ((a & 1)==0) {
// If b is even
if ((b & 1)==0)
return false;
// If b is odd
else
return (n % 3 != 0);
}
// If a is odd
else {
// If b is odd
if ((b & 1)==0)
return ((n - 1) % 3 != 0);
// If b is eve
else
return ((n + 1) % 3 != 0);
}
}
// Driven Program
static void Main()
{
int a = 2, b = 4;
int n = 3;
if(findNature(a, b, n))
System.Console.WriteLine("Odd");
else
System.Console.WriteLine("Even");
}
}
// This Code is contributed by mits
PHP
输出:
Even
方法2(有效):
可以看到,第n个项的性质(奇数或偶数)取决于先前的项,而上一个项的性质取决于其先前的项,最后取决于初始值,即0和1 。
因此,对于0和1 ,我们有四种可能的情况:
情况1:当一个0和一个1是偶数时
在这种情况下,序列中的每个值都将仅是偶数。
情况2:当0和1为奇数时
在这种情况下,观察一个2是偶数,一个3是奇数,一个4是奇数,依此类推。因此,我们可以说我是即使我是形式3 * K – 1,否则奇怪。
情况3:当0为偶数而1为奇数时
在这种情况下,观察一个2为奇数,一个3为偶数,一个4和一个5为奇数,一个6为偶数,依此类推。因此,我们可以说,一个我是我就算是多的3,其他奇
情况4:当0为奇数而1为偶数时
在这种情况下,观察一个2和一个3为奇数,一个4为偶数,一个5和一个6为奇数,一个7为偶数,依此类推。因此,我们可以说,一个i是即使i是形式3 * K + 1,K> = 0的,否则奇数。
以下是此方法的实现:
C++
// CPP Program to check if the nth is odd or even in a
// sequence where each term is sum of previous two term
#include
using namespace std;
// Return if the nth term is even or odd.
bool findNature(int a, int b, int n)
{
if (n == 0)
return (a & 1);
if (n == 1)
return (b & 1);
// If a is even
if (!(a & 1)) {
// If b is even
if (!(b & 1))
return false;
// If b is odd
else
return (n % 3 != 0);
}
// If a is odd
else {
// If b is odd
if (!(b & 1))
return ((n - 1) % 3 != 0);
// If b is eve
else
return ((n + 1) % 3 != 0);
}
}
// Driven Program
int main()
{
int a = 2, b = 4;
int n = 3;
(findNature(a, b, n) ? (cout << "Odd"
<< " ")
: (cout << "Even"
<< " "));
return 0;
}
Java
// Java Program to check if the nth is odd or even in a
// sequence where each term is sum of previous two term
class GFG{
// Return if the nth term is even or odd.
static boolean findNature(int a, int b, int n)
{
if (n == 0)
return (a & 1)==1?true:false;
if (n == 1)
return (b & 1)==1?true:false;
// If a is even
if ((a & 1)==0) {
// If b is even
if ((b & 1)==0)
return false;
// If b is odd
else
return (n % 3 != 0);
}
// If a is odd
else {
// If b is odd
if ((b & 1)==0)
return ((n - 1) % 3 != 0);
// If b is eve
else
return ((n + 1) % 3 != 0);
}
}
// Driven Program
public static void main(String[] args)
{
int a = 2, b = 4;
int n = 3;
if(findNature(a, b, n))
System.out.println("Odd");
else
System.out.println("Even");
}
}
// This Code is contributed by mits
Python3
# Python3 Program to check if the
# nth is odd or even in a
# sequence where each term is
# sum of previous two term
# Return if the nth
# term is even or odd.
def findNature(a, b, n):
if (n == 0):
return (a & 1);
if (n == 1):
return (b & 1);
# If a is even
if ((a & 1) == 0):
# If b is even
if ((b & 1) == 0):
return False;
# If b is odd
else:
return True if(n % 3 != 0) else False;
# If a is odd
else:
# If b is odd
if ((b & 1) == 0):
return True if((n - 1) % 3 != 0) else False;
# If b is eve
else:
return True if((n + 1) % 3 != 0) else False;
# Driver Code
a = 2;
b = 4;
n = 3;
if (findNature(a, b, n) == True):
print("Odd", end = " ");
else:
print("Even", end = " ");
# This code is contributed by mits
C#
// C# Program to check if the nth is odd or even in a
// sequence where each term is sum of previous two term
class GFG{
// Return if the nth term is even or odd.
static bool findNature(int a, int b, int n)
{
if (n == 0)
return (a & 1)==1?true:false;
if (n == 1)
return (b & 1)==1?true:false;
// If a is even
if ((a & 1)==0) {
// If b is even
if ((b & 1)==0)
return false;
// If b is odd
else
return (n % 3 != 0);
}
// If a is odd
else {
// If b is odd
if ((b & 1)==0)
return ((n - 1) % 3 != 0);
// If b is eve
else
return ((n + 1) % 3 != 0);
}
}
// Driven Program
static void Main()
{
int a = 2, b = 4;
int n = 3;
if(findNature(a, b, n))
System.Console.WriteLine("Odd");
else
System.Console.WriteLine("Even");
}
}
// This Code is contributed by mits
的PHP
输出 :
Even