📌  相关文章
📜  检查给定数字的所有数字是否相同

📅  最后修改于: 2022-05-13 01:56:07.015000             🧑  作者: Mango

检查给定数字的所有数字是否相同

给定一个正整数N ,任务是检查给定整数N的所有数字是否相同。如果发现是真的,则打印Yes 。否则,打印No

例子:

朴素方法:解决给定问题的最简单方法是遍历给定数字N的所有数字,如果存在任何不同的数字,则打印Yes 。否则,打印No

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to check if all the digits
// in the number N is the same or not
string checkSameDigits(int N)
{
   
    // Find the last digit
    int digit = N % 10;
 
    while (N != 0)
    {
 
        // Find the current last digit
        int current_digit = N % 10;
 
        // Update the value of N
        N = N / 10;
 
        // If there exists any distinct
        // digit, then return No
        if (current_digit != digit)
        {
            return "No";
        }
    }
 
    // Otherwise, return Yes
    return "Yes";
}
 
// Driver Code
int main()
{
    int N = 222;
    cout << (checkSameDigits(N));
    return 0;
}
 
// This code is contributed by Potta Lokesh


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to check if all the digits
    // in the number N is the same or not
    public static String checkSameDigits(int N)
    {
        // Find the last digit
        int digit = N % 10;
 
        while (N != 0) {
 
            // Find the current last digit
            int current_digit = N % 10;
 
            // Update the value of N
            N = N / 10;
 
            // If there exists any distinct
            // digit, then return No
            if (current_digit != digit) {
                return "No";
            }
        }
 
        // Otherwise, return Yes
        return "Yes";
    }
 
    // Driver Code
    public static void main(String args[])
        throws IOException
    {
        int N = 222;
        System.out.println(
            checkSameDigits(N));
    }
}


Python3
# Python Program to implement
# the above approach
 
# Function to check if all the digits
# in the number N is the same or not
def checkSameDigits(N) :
   
    # Find the last digit
    digit = N % 10;
 
    while (N != 0) :
 
        # Find the current last digit
        current_digit = N % 10;
 
        # Update the value of N
        N = N // 10;
 
        # If there exists any distinct
        # digit, then return No
        if (current_digit != digit) :
            return "No";
 
    # Otherwise, return Yes
    return "Yes";
 
# Driver Code
if __name__ == "__main__" :
 
    N = 222;
    print(checkSameDigits(N));
   
 
    # This code is contributed by AnkThon


C#
// C# Program to implement
// the above approach
using System;
class GFG {
 
    // Function to check if all the digits
    // in the number N is the same or not
    static string checkSameDigits(int N)
    {
 
        // Find the last digit
        int digit = N % 10;
 
        while (N != 0) {
 
            // Find the current last digit
            int current_digit = N % 10;
 
            // Update the value of N
            N = N / 10;
 
            // If there exists any distinct
            // digit, then return No
            if (current_digit != digit) {
                return "No";
            }
        }
 
        // Otherwise, return Yes
        return "Yes";
    }
 
    // Driver Code
    public static void Main()
    {
        int N = 222;
        Console.Write(checkSameDigits(N));
    }
}
 
// This code is contributed by divyesh972019.


Javascript


C++
#include 
using namespace std;
 
// Function to check if all the digits
// in the number N is the same or not
string checkSameDigits(int N)
{
   
    // Get the length of N
    int length = (N) + 1;
 
    // Form the number M of the type
    // K*111... where K is the
    // rightmost digit of N
    int M = ((10, length) - 1) / (10 - 1);
    M *= N % 10;
 
    // Check if the numbers are equal
    if (M = N)
        return "Yes";
 
    // Otherwise
    return "No";
}
 
// Driver Code
int main()
 
{
    int N = 222;
    cout << checkSameDigits(N);
}
 
// This code is contributed by shivanisinghss2110


Java
// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to check if all the digits
    // in the number N is the same or not
    public static String checkSameDigits(int N)
    {
        // Get the length of N
        int length = ((int)Math.log10(N)) + 1;
 
        // Form the number M of the type
        // K*111... where K is the
        // rightmost digit of N
        int M = ((int)Math.pow(10, length) - 1)
                / (10 - 1);
        M *= N % 10;
 
        // Check if the numbers are equal
        if (M == N)
            return "Yes";
 
        // Otherwise
        return "No";
    }
 
    // Driver Code
    public static void main(String args[])
        throws IOException
    {
        int N = 222;
        System.out.println(
            checkSameDigits(N));
    }
}


Python3
# Python3 program for the above approach
import math
 
# Function to check if all the digits
# in the number N is the same or not
def checkSameDigits(N) :
     
    # Get the length of N
    length = int(math.log10(N)) + 1;
 
    # Form the number M of the type
    # K*111... where K is the
    # rightmost digit of N
    M = (int(math.pow(10, length)) - 1)// (10 - 1);
    M *= N % 10;
 
    # Check if the numbers are equal
    if (M == N) :
        return "Yes";
 
    # Otherwise
    return "No";
 
    # Driver Code
if __name__ == "__main__" :
     
    N = 222;
    print(checkSameDigits(N));
     
    # This code is contributed by AnkThon


C#
// C# program for the above approach
 
using System;
 
class GFG {
 
    // Function to check if all the digits
    // in the number N is the same or not
    public static String checkSameDigits(int N)
    {
        // Get the length of N
        int length = ((int)Math.Log10(N)) + 1;
 
        // Form the number M of the type
        // K*111... where K is the
        // rightmost digit of N
        int M = ((int)Math.Pow(10, length) - 1) / (10 - 1);
        M *= N % 10;
 
        // Check if the numbers are equal
        if (M == N)
            return "Yes";
 
        // Otherwise
        return "No";
    }
 
    // Driver Code
    public static void Main()
    {
        int N = 222;
        Console.WriteLine(checkSameDigits(N));
    }
}
 
// This code is contributed by subhammahato348.


Javascript



输出:
Yes

时间复杂度: O(log 10 N)
辅助空间: O(1)

有效方法:上述方法也可以通过形成另一个数字来优化,比如给定数字N的相同长度的M与 N 的最右边的数字假设N具有所有相同的数字,然后将其与 N 进行比较。现在,M 是type (K*111….) ,其中KN中的任何数字。

现在要创建仅由1组成的数字M ,可以使用几何级数的总和,如图所示,数字计数为 3:

根据以上观察,生成数字M并检查K*M是否与N相同。如果发现是真的,则打印Yes 。否则,打印No

下面是上述方法的实现:

C++

#include 
using namespace std;
 
// Function to check if all the digits
// in the number N is the same or not
string checkSameDigits(int N)
{
   
    // Get the length of N
    int length = (N) + 1;
 
    // Form the number M of the type
    // K*111... where K is the
    // rightmost digit of N
    int M = ((10, length) - 1) / (10 - 1);
    M *= N % 10;
 
    // Check if the numbers are equal
    if (M = N)
        return "Yes";
 
    // Otherwise
    return "No";
}
 
// Driver Code
int main()
 
{
    int N = 222;
    cout << checkSameDigits(N);
}
 
// This code is contributed by shivanisinghss2110

Java

// Java program for the above approach
 
import java.io.*;
 
class GFG {
 
    // Function to check if all the digits
    // in the number N is the same or not
    public static String checkSameDigits(int N)
    {
        // Get the length of N
        int length = ((int)Math.log10(N)) + 1;
 
        // Form the number M of the type
        // K*111... where K is the
        // rightmost digit of N
        int M = ((int)Math.pow(10, length) - 1)
                / (10 - 1);
        M *= N % 10;
 
        // Check if the numbers are equal
        if (M == N)
            return "Yes";
 
        // Otherwise
        return "No";
    }
 
    // Driver Code
    public static void main(String args[])
        throws IOException
    {
        int N = 222;
        System.out.println(
            checkSameDigits(N));
    }
}

Python3

# Python3 program for the above approach
import math
 
# Function to check if all the digits
# in the number N is the same or not
def checkSameDigits(N) :
     
    # Get the length of N
    length = int(math.log10(N)) + 1;
 
    # Form the number M of the type
    # K*111... where K is the
    # rightmost digit of N
    M = (int(math.pow(10, length)) - 1)// (10 - 1);
    M *= N % 10;
 
    # Check if the numbers are equal
    if (M == N) :
        return "Yes";
 
    # Otherwise
    return "No";
 
    # Driver Code
if __name__ == "__main__" :
     
    N = 222;
    print(checkSameDigits(N));
     
    # This code is contributed by AnkThon

C#

// C# program for the above approach
 
using System;
 
class GFG {
 
    // Function to check if all the digits
    // in the number N is the same or not
    public static String checkSameDigits(int N)
    {
        // Get the length of N
        int length = ((int)Math.Log10(N)) + 1;
 
        // Form the number M of the type
        // K*111... where K is the
        // rightmost digit of N
        int M = ((int)Math.Pow(10, length) - 1) / (10 - 1);
        M *= N % 10;
 
        // Check if the numbers are equal
        if (M == N)
            return "Yes";
 
        // Otherwise
        return "No";
    }
 
    // Driver Code
    public static void Main()
    {
        int N = 222;
        Console.WriteLine(checkSameDigits(N));
    }
}
 
// This code is contributed by subhammahato348.

Javascript



输出:
Yes

时间复杂度: O(1)
辅助空间: O(1)