📜  波动数字

📅  最后修改于: 2021-04-23 22:27:19             🧑  作者: Mango

波动数字是仅具有两种数字类型的数字,而替代数字是相同的,即,其形式为“ ababab…。”。有时它被限制为要求至少具有3位数字且a不等于b的非平凡的起伏数。

  1. 对于任何n> = 3,有9×9 = 81个非平凡的n位数字起伏数,因为第一个数字可以有9个值(不能为0),而第二个数字必须为9个值与第一个不同。

给定一个数字,请考虑交替数字的定义,检查它是否为波动数字,至少3个数字和相邻数字不相同。
例子 :

Input : n = 121
Output : Yes

Input : n = 1991
Output : No
C++
// C++ program to check whether a number
// is undulating or not
#include 
using namespace std;
 
bool isUndulating(string n)
{
    // Considering the definition
    // with restriction that there
    // should be at least 3 digits
    if (n.length() <= 2)
       return false;
 
    // Check if all alternate digits are
    // same or not.
    for (int i = 2; i < n.length(); i++)
        if (n[i - 2] != n[i])
           false;
 
    return true;
}
 
int main()
{
    string n = "1212121";
    if (isUndulating(n))
        cout << "Yes";
    else
        cout << "No";
}


Java
// Java program to check whether a number
// is undulating or not
import java.util.*;
 
class GFG {
     
    public static boolean isUndulating(String n)
    {
         
        // Considering the definition
        // with restriction that there
        // should be at least 3 digits
            if (n.length() <= 2)
                return false;
     
        // Check if all alternate digits are
        // same or not.
        for (int i = 2; i < n.length(); i++)
            if (n.charAt(i-2) != n.charAt(i))
                return false;
     
        return true;
    }
     
     
    // Driver code
    public static void main (String[] args)
    {
         
        String n = "1212121";
         
        if (isUndulating(n)==true)
            System.out.println("yes");
        else
            System.out.println("no");
    }
}
 
// This code is contributed by akash1295.


Python3
# Python3 program to check whether a
# number is undulating or not
 
def isUndulating(n):
 
    # Considering the definition
    # with restriction that there
    # should be at least 3 digits
    if (len(n) <= 2):
        return False
 
    # Check if all alternate digits
    # are same or not.
    for i in range(2, len(n)):
        if (n[i - 2] != n[i]):
            return False
 
    return True
 
# Driver Code
n = "1212121"
if (isUndulating(n)):
    print("Yes")
else:
    print("No")
 
# This code is contributed by Smitha Dinesh Semwal.


C#
// C# program to check whether a number
// is undulating or not
using System;
 
class GFG {
     
    public static bool isUndulating(string n)
    {
         
        // Considering the definition
        // with restriction that there
        // should be at least 3 digits
            if (n.Length <= 2)
                return false;
     
        // Check if all alternate digits are
        // same or not.
        for (int i = 2; i < n.Length; i++)
            if (n[i-2] != n[i])
                return false;
     
        return true;
    }
     
    // Driver code
    public static void Main ()
    {
         
        string n = "1212121";
         
        if (isUndulating(n)==true)
            Console.WriteLine("yes");
        else
            Console.WriteLine("no");
    }
}
 
// This code is contributed by Vt_m.


PHP


Javascript


输出:
Yes