📜  交替数字

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

给定数字N ,任务是检查N是否为交替数字。如果N是一个交替数字,则打印“是”,否则打印“否”

例子:

方法:想法是将数字转换为字符串,并检查是否有任何数字后跟相同奇偶校验的数字,则N不是交替数,否则N交替数

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if a string is
// of the form even odd even odd...
bool isEvenOddForm(string s)
{
    int n = s.length();
    for (int i = 0; i < n; i++) {
        if (i % 2 == 0 && s[i] % 2 != 0)
            return false;
 
        if (i % 2 == 1 && s[i] % 2 != 1)
            return false;
    }
    return true;
}
 
// Function to check if a string is
// of the form odd even odd even  ...
bool isOddEvenForm(string s)
{
    int n = s.length();
    for (int i = 0; i < n; i++) {
        if (i % 2 == 0 && s[i] % 2 != 1)
            return false;
 
        if (i % 2 == 1 && s[i] % 2 != 0)
            return false;
    }
    return true;
}
 
// Function to check if n is an
// alternating number
bool isAlternating(int n)
{
    string str = to_string(n);
    return (isEvenOddForm(str)
            || isOddEvenForm(str));
}
 
// Driver Code
int main()
{
    // Given Number N
    int N = 129;
 
    // Function Call
    if (isAlternating(N))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java program for the above approach
class GFG{
     
// Function to check if a string is
// of the form even odd even odd...
static boolean isEvenOddForm(String s)
{
    int n = s.length();
    for(int i = 0; i < n; i++)
    {
       if (i % 2 == 0 && s.charAt(i) % 2 != 0)
           return false;
       if (i % 2 == 1 && s.charAt(i) % 2 != 1)
           return false;
    }
    return true;
}
 
// Function to check if a string is
// of the form odd even odd even ...
static boolean isOddEvenForm(String s)
{
    int n = s.length();
    for(int i = 0; i < n; i++)
    {
       if (i % 2 == 0 && s.charAt(i) % 2 != 1)
           return false;
       if (i % 2 == 1 && s.charAt(i) % 2 != 0)
           return false;
    }
    return true;
}
 
// Function to check if n is an
// alternating number
static boolean isAlternating(int n)
{
    String str = Integer.toString(n);
    return (isEvenOddForm(str) ||
            isOddEvenForm(str));
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given number N
    int N = 129;
 
    // Function call
    if (isAlternating(N))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This Code is contributed by rock_cool


Python3
# Python3 program for the above approach
 
# Function to check if a string is
# of the form even odd even odd...
def isEvenOddForm(s):
    n = len(s)
    for i in range(n):
        if (i % 2 == 0 and
            int(s[i]) % 2 != 0):
            return False
        if (i % 2 == 1 and
            int(s[i]) % 2 != 1):
            return False
    return True
 
# Function to check if a string is
# of the form odd even odd even ...
def isOddEvenForm(s):
    n = len(s)
    for i in range(n):
        if (i % 2 == 0 and
            int(s[i]) % 2 != 1):
            return False
        if (i % 2 == 1 and
            int(s[i]) % 2 != 0):
            return False
    return True
 
# Function to check if n is an
# alternating number
def isAlternating(n):
    s = str(n)
    return (isEvenOddForm(s) or
            isOddEvenForm(s))
 
# Driver Code
 
# Given Number N
N = 129
 
# Function Call
if (isAlternating(N)):
    print("Yes")
else:
    print("No")
     
# This code is contributed by Vishal Maurya


C#
// C# program for the above approach
using System;
class GFG{
     
// Function to check if a string is
// of the form even odd even odd...
static bool isEvenOddForm(String s)
{
    int n = s.Length;
    for(int i = 0; i < n; i++)
    {
        if (i % 2 == 0 && s[i] % 2 != 0)
            return false;
        if (i % 2 == 1 && s[i] % 2 != 1)
            return false;
    }
    return true;
}
 
// Function to check if a string is
// of the form odd even odd even ...
static bool isOddEvenForm(String s)
{
    int n = s.Length;
    for(int i = 0; i < n; i++)
    {
        if (i % 2 == 0 && s[i] % 2 != 1)
            return false;
        if (i % 2 == 1 && s[i] % 2 != 0)
            return false;
    }
    return true;
}
 
// Function to check if n is an
// alternating number
static bool isAlternating(int n)
{
    String str = n.ToString();
    return (isEvenOddForm(str) ||
            isOddEvenForm(str));
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given number N
    int N = 129;
 
    // Function call
    if (isAlternating(N))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:
Yes

时间复杂度: O(log 10 N)