📜  自我描述号码

📅  最后修改于: 2021-04-26 18:05:39             🧑  作者: Mango

一个自描述数字是整数n,在给定的底数b内是b位数字,其中位置p处的每个数字(最高有效位在位置0处,最低有效位在位置b-1处)计算一个数字p的次数。在n中。
例如,在以10为底的数字中, 6210001000是一个自描述数字。

解释 :
以10为基数的10位数字。
它在位置0处有6,在6210001000中有六个0。
它在位置1有2,在6210001000中有两个1。
它在位置2有1,而在6210001000中有2。
它在位置3处为0,在6210001000中为零3s。
它在位置4处为0,而在6210001000中为零4s。
它在位置5处为0,而在6210001000中为零5s。
它在位置6有1,在6210001000中有一个6。
它在位置7处为0,而在6210001000中为零7s。
它在位置8处为0,在6210001000中为零8s。
它在位置9处为0,而在6210001000中为零。
[资料来源:维基百科]

程序说明:
1。首先,所有数字都从外循环中提取出来,并在每次迭代中存储在变量b中。
2。然后在内部循环中,对字符串存在i次数(这是外部循环的第i个索引)进行计数。
3。最后,将该计数与存储在变量b中的第i个索引处的数字进行比较。

C++
// C++ program to print
// all self descriptive
// number below 100000000
#include 
using namespace std;
 
bool isSelfDescriptiveNumber(int num)
{
    // converting the integer
    // num to string
    string s = to_string(num);
    for (int i = 0;
             i < s.size(); i++)
    {
 
        // Extracting each digit
        // one by one from the
        // string
        char temp_char = s.at(i);
 
        // converting the string
        // (digit) into integer b
        // variable stores the digit
        // present at index 'i'
        int b = temp_char - '0';
 
        // counting how many
        // times the particular
        // digit occur in the
        // whole number "num"
        int count = 0;
        for (int j = 0;
                 j < s.size(); j++)
        {
            // converting string
            // char to integer
            int temp = s.at(j) - '0';
 
            // checking whether it is
            // equal to the index 'i'
            // if it is then increment
            // the count .
            if (temp == i)
            {
                count++;
            }
        }
         
        // If it is not equal
        // it return false .
        if (count != b)
            return false;
    }
    return true;
}
 
// Driver Code
int main()
{
    for (int i = 0; i < 100000000; i++)
        if (isSelfDescriptiveNumber(i))
            cout << i << endl;
}
 
// This code is contributed by
// Manish Shaw(manishshaw1)


Java
// Java program to print all self descriptive
// number below 100000000
public class SelfDescriptive {
 
    public static boolean isSelfDescriptiveNumber(int num)
    {
        // converting the integer num to string
        String s = Integer.toString(num);
        for (int i = 0; i < s.length(); i++) {
 
            // Extracting each digit one by one from the string
            String temp_char = s.charAt(i) + "";
 
            // converting the string (digit) into integer
            // b variable stores the digit present at index 'i'
            int b = Integer.parseInt(temp_char);
 
            // counting how many times the particular digit
            // occur in the whole number "num"
            int count = 0;
            for (int j = 0; j < s.length(); j++) {
                // converting string char to integer
                int temp = Integer.parseInt(s.charAt(j) + "");
 
                // checking whether it is equal to the index 'i'
                // if it is then increment the count .
                if (temp == i) {
                    count++;
                }
            }
 
            // If it is not equal
            // it return false .
            if (count != b)
                return false;
        }
        return true;
    }
 
    public static void main(String[] args)
    {
        for (int i = 0; i < 100000000; i++)
            if (isSelfDescriptiveNumber(i))
                System.out.println(i);       
    }
}


Python3
# Python3 program to print
# all self descriptive
# number below 100000000
def isSelfDescriptiveNumber(num):
 
    # Converting the integer
    # num to string
    s = str(num)
     
    for i in range(len(s)):
  
        # Extracting each digit
        # one by one from the
        # string
        temp_char = s[i]
  
        # Converting the string
        # (digit) into integer b
        # variable stores the digit
        # present at index 'i'
        b = ord(temp_char) - ord('0')
  
        # Counting how many
        # times the particular
        # digit occur in the
        # whole number "num"
        count = 0
         
        for j in range(len(s)):
             
            # Converting string
            # char to integer
            temp = ord(s[j]) - ord('0')
  
            # Checking whether it is
            # equal to the index 'i'
            # if it is then increment
            # the count .
            if (temp == i):
                count += 1
                 
        # If it is not equal
        # it return false .
        if (count != b):
            return False
     
    return True
 
# Driver code
if __name__=="__main__":
     
    for i in range(100000000):
        if (isSelfDescriptiveNumber(i)):
            print(i)
 
# This code is contributed by rutvik_56


C#
// C# program to print
// all self descriptive
// number below 100000000
using System;
  
class GFG
{
static bool isSelfDescriptiveNumber(int num)
{
    // converting the integer
    // num to string
    string s = num.ToString();
    for (int i = 0;
             i < s.Length; i++)
    {
 
        // Extracting each digit
        // one by one from the
        // string
        string temp_char = s[i] + "";
 
        // converting the string
        // (digit) into integer b
        // variable stores the digit
        // present at index 'i'
        int b = int.Parse(temp_char);
 
        // counting how many
        // times the particular
        // digit occur in the
        // whole number "num"
        int count = 0;
        for (int j = 0;
                 j < s.Length; j++)
        {
            // converting string
            // char to integer
            int temp = int.Parse(s[j] + "");
 
            // checking whether it is
            // equal to the index 'i'
            // if it is then increment
            // the count .
            if (temp == i)
            {
                count++;
            }
        }
          
        // If it is not equal
        // it return false .
        if (count != b)
            return false;
    }
    return true;
}
  
// Driver Code
static void Main()
{
    for (int i = 0; i < 100000000; i++)
        if (isSelfDescriptiveNumber(i))
            Console.WriteLine(i);   
}
}
  
// This code is contributed by
// Manish Shaw(manishshaw1)


PHP


输出:

1210
2020
21200
3211000