📜  综合症编号

📅  最后修改于: 2021-04-21 20:46:46             🧑  作者: Mango

给定整数N ,任务是检查N是否为合子数。

例子:

方法:想法是将数字转换为字符串,如果任何字符不等于前一个字符和下一个字符,则返回false。由于第一个字符没有前一个字符,最后一个字符没有下一个字符,因此我们将在字符串的开头和末尾添加一个空格。
下面是上述方法的实现:

C++
// C++ implementation to check if N
// is an zygodrome number
 
#include 
using namespace std;
 
// Function to check if N
// is an zygodrome number
bool iszygodromeNum(int N)
{
    // convert N to string
    string s = to_string(N);
 
    // Adding a space at the
    // beginning and
    // end of the string
    s = ' ' + s + ' ';
    // Traverse the string
    for (int i = 1; i < s.size() - 1; i++) {
        // If any character is not same as
        // prev and next then return false
        if (s[i] != s[i - 1]
            && s[i] != s[i + 1]) {
            return false;
        }
    }
    return true;
}
 
// Driver code
int main()
{
    int n = 1122;
    if (iszygodromeNum(n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java implementation to check if N
// is a zygodrome number
class GFG{
 
// Function to check if N
// is an zygodrome number
static boolean iszygodromeNum(int N)
{
    // convert N to string
    String s = Integer.toString(N);
 
    // Adding a space at the
    // beginning and
    // end of the string
    s = ' ' + s + ' ';
     
    // Traverse the string
    for (int i = 1; i < s.length() - 1; i++)
    {
        // If any character is not same as
        // prev and next then return false
        if (s.charAt(i) != s.charAt(i - 1) &&
            s.charAt(i) != s.charAt(i + 1))
        {
            return false;
        }
    }
    return true;
}
 
// Driver code
public static void main(String[] args)
{
    int n = 1122;
    if (iszygodromeNum(n))
        System.out.println("Yes");
    else
        System.out.println("No");
}
}
 
// This code is contributed by shubham


Python3
# Python3 program implementation to check
# if N is an zygodrome number
 
# Function to check if N
# is an zygodrome number
def iszygodromeNum(N):
     
    # Convert N to string
    s = str(N);
 
    # Adding a space at the
    # beginning and
    # end of the string
    s = ' ' + s + ' ';
     
    # Traverse the string
    i = 1
    while i < len(s) - 1:
     
        # If any character is not same as
        # prev and next then return false
        if ((s[i] != s[i - 1]) and
            (s[i] != s[i + 1])):
            return False;
             
        i += 1
         
    return True;
 
# Driver code
if __name__ == '__main__':
     
    n = 1122;
     
    if iszygodromeNum(n):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by jana_sayantan


C#
// C# implementation to check if N
// is a zygodrome number
using System;
class GFG{
 
// Function to check if N
// is an zygodrome number
static bool iszygodromeNum(int N)
{
    // convert N to string
    String s = N.ToString();
 
    // Adding a space at the
    // beginning and
    // end of the string
    s = ' ' + s + ' ';
     
    // Traverse the string
    for (int i = 1; i < s.Length - 1; i++)
    {
        // If any character is not same as
        // prev and next then return false
        if (s[i] != s[i - 1] &&
            s[i] != s[i + 1])
        {
            return false;
        }
    }
    return true;
}
 
// Driver code
public static void Main(String[] args)
{
    int n = 1122;
    if (iszygodromeNum(n))
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:
Yes