📜  非平凡的无数

📅  最后修改于: 2021-04-29 06:16:18             🧑  作者: Mango

给定一个整数N ,任务是检查N是否为非平凡的不重复数。

例子:

方法:想法是将数字转换为字符串。如果字符串的长度是偶数,那么我们将检查字符串的前半部分是否等于后半部分。如果长度为奇数,我们将在最后的字符串添加字符串的第2个字符,使它的长度均匀。最后,检查字符串的前半部分是否等于后半部分。

下面是上述方法的实现:

C++
// C++ implementation to check if N 
// is a Nontrivial undulant number
  
#include
using namespace std;
  
  
// Function to check if a string 
// is double string or not
bool isDouble(int num)
{
    string s = to_string(num);
    int l = s.length();
      
    // a and b shoud not be equal
    if(s[0] == s[1])
       return false;
          
    // Conditio to check 
    // if length is odd
    // make length even
    if(l % 2 == 1)
    {
        s = s + s[1];
        l++;
    }
          
    // first half of s
    string s1 = s.substr(0, l/2);
    // second half of s
    string s2 = s.substr(l/2);
          
    // Double string if first
    // and last half are equal
    return s1 == s2;
}
  
// Function to check if N is an 
// Nontrivial undulant number
bool isNontrivialUndulant(int N)
{    
    return N > 100 && isDouble(N);
}
  
// Driver Code
int main()
{
    int n = 121;
    if (isNontrivialUndulant(n))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java implementation to check if N
// is a Nontrivial undulant number
class GFG{ 
  
// Function to check if a string 
// is double string or not
static boolean isDouble(int num)
{
    String s = Integer.toString(num);
    int l = s.length();
      
    // a and b shoud not be equal
    if(s.charAt(0) == s.charAt(1))
       return false;
          
    // Conditio to check if length 
    // is odd make length even
    if(l % 2 == 1)
    {
        s = s + s.charAt(1);
        l++;
    }
          
    // First half of s
    String s1 = s.substring(0, l / 2);
      
    // Second half of s
    String s2 = s.substring(l / 2);
          
    // Double string if first
    // and last half are equal
    return s1.equals(s2);
}
  
// Function to check if N is an 
// Nontrivial undulant number
static boolean isNontrivialUndulant(int N)
{ 
    return N > 100 && isDouble(N);
}
  
// Driver code 
public static void main(String[] args) 
{ 
    int n = 121;
      
    if (isNontrivialUndulant(n))
    {
        System.out.println("Yes");
    }
    else
    {
        System.out.println("No");
    }
} 
} 
  
// This code is contributed by shubham


Python3
# Python3 implementation to check if N 
# is a Nontrivial undulant number
  
# Function to check if a string 
# is double string or not
def isDouble(num):
  
    s = str(num)
    l = len(s)
  
    # a and b shoud not be equal
    if(s[0] == s[1]):
        return False
  
    # Condition to check
    # if length is odd
    # make length even
    if(l % 2 == 1):
  
        s = s + s[1]
        l += 1
  
    # First half of s
    s1 = s[:l // 2]
  
    # Second half of s
    s2 = s[l // 2:]
  
    # Double string if first
    # and last half are equal
    return s1 == s2
  
# Function to check if N is an 
# Nontrivial undulant number
def isNontrivialUndulant(N):
  
    return N > 100 and isDouble(N)
  
# Driver Code
n = 121
  
if (isNontrivialUndulant(n)):
    print("Yes")
else:
    print("No")
      
# This code is contributed by vishu2908


C#
// C# implementation to check if N
// is a Nontrivial undulant number
using System;
class GFG{ 
   
// Function to check if a string 
// is double string or not
static bool isDouble(int num)
{
    String s = num.ToString();
    int l = s.Length;
       
    // a and b shoud not be equal
    if(s[0] == s[1])
       return false;
           
    // Conditio to check if length 
    // is odd make length even
    if(l % 2 == 1)
    {
        s = s + s[1];
        l++;
    }
           
    // First half of s
    String s1 = s.Substring(0, l / 2);
       
    // Second half of s
    String s2 = s.Substring(l / 2);
           
    // Double string if first
    // and last half are equal
    return s1.Equals(s2);
}
   
// Function to check if N is an 
// Nontrivial undulant number
static bool isNontrivialUndulant(int N)
{ 
    return N > 100 && isDouble(N);
}
   
// Driver code 
public static void Main(String[] args) 
{ 
    int n = 121;
       
    if (isNontrivialUndulant(n))
    {
        Console.WriteLine("Yes");
    }
    else
    {
        Console.WriteLine("No");
    }
} 
} 
  
// This code is contributed by gauravrajput1


输出:
Yes

参考文献: OEIS