📜  可逆数

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

如果数字之和与它的反数之和只有奇数个数字,则该数字被认为是可逆的。问题是找出数字是否可逆。

例子:

Input: 36 
Output: Reversible number
as 36 + 63 = 99 has only odd digits.

Input: 409 
Output: Reversible number
as 409 + 904 = 1313 has only odd digits.

Input: 35 
Output: Not Reversible number
as 35 + 53 = 88 has only odd digits

天真的方法

计算每个数字的倒数并将其添加到数字中。如果结果是可逆的,则增加count的值。计算从1到n的每个数字。

时间复杂度: O(10 ^ n),因为它应该计算每个数字的倒数。

进阶方式

  • 1位数字:任何一位数字都会加起来,它始终是偶数,因此没有解决方案。
  • 2位数字:两位数字必须为奇数。
    • 如果a + b> 10,那么我们有一个结转,因此结果的第一位数将与第二位数具有不同的奇偶校验。
    • 因此,只能在a + b <10且a + b为奇数的情况下形成解决方案。因此,总共有20个这样的数字是可能的。
  • 3位数:
    • 中间数字需要添加到自己。这意味着第三位数字必须带有一个结余且为奇数。
    • 由于第三位数字是奇数,因此如果第二位数字没有残留,则第一位数字也是奇数,这在第二位数字小于5时发生,这给我们提供了第一/第三位数字集的20种选择和5种选择因此,总共有100对。
  • 4位数字:有两对,分别是内对和外对。
    • 如果内部对具有残留物,则外部对也必须具有残留物。
    • 否则,两个内部对将具有不同的奇偶校验。
    • 如果内部对具有结转,那么外部对将具有不同的奇偶校验,因为第一个数字将以最后一个数字不会得到的结转结束。
    • 因此,只有在没有任何对结转时,我们才有解决方案。
    • 总计:对于外线对,这给了我们在两位数情况下看到的20个选择。由于内部对也可以包含零,因此它为我们提供了30种情况。
      或总共我们得到20 * 30 = 600个解决方案。
  • 5、9、13 ..位数: 1位数字无解。
  • 6、8、10 ..位数字:与2位数字相同,即,如果n = 2 * k,则总解= 20 * 30 ^(k-1)。
  • 7、11、15 ..位数字:与3位数字相同,即,如果n = 4k + 3,则总解= 100 * 500 ^(k)。

推广解决方案:

  • 所有偶数数字(2、4、6、8 ..)具有相同的公式,因此我们可以一概而论
    对于某个整数k使得n = 2k,我们有20 * 30 ^(k-1)个解
    代表外部对以及所有内部对。
  • 对于形式为4k + 3(k是整数)的n(3、7、11 ..),我们得到中间的数字和
    外部对为我们提供5和20个选项,例如3位数的数字。
    然后,我们有一组内部对,这给我们提供了20和25个解决方案。
    因此这意味着我们可以将其推广为20 * 5 *(20 * 25)^(k)= 100 * 500 ^(k)。
  • 对于形式为4k + 1的n表示1、5、9。这些都不具有任何解。

程序检查数字是否可逆

C++
// C++ program to check if a given
// number is reversible or not
#include 
#include 
using namespace std;
 
// Function to check reversible number
void checkReversible (int n)
{
    int rev = 0, rem;
 
    // Calculate reverse of n
    int flag = n;
    while (flag)
    {
        rem = flag % 10;
        rev *= 10;
        rev += rem;
        flag /= 10;
    }
 
    // Calculate sum of number
    // and its reverse
    int sum = rev + n;
 
    // Check for reverse number
    // reach digit must be odd
    while (sum && (rem % 2 != 0))
    {
        rem = sum % 10;
        sum /= 10;
    }
 
    if (sum == 0)
        cout << "Reversible Number";
    else
        cout << "Non-Reversible Number";
}
 
// Driver function
int main()
{
    int n = 36;
    checkReversible(n);
    return 0;
}


Java
// Java program to check if a given
// number is reversible or not
import java.io.*;
 
class GFG {
     
    // Function to check reversible number
    static void checkReversible (int n)
    {
        int rev = 0, rem = 0;
     
        // Calculate reverse of n
        int flag = n;
        while (flag>0)
        {
            rem = flag % 10;
            rev *= 10;
            rev += rem;
            flag /= 10;
        }
     
        // Calculate sum of number
        // and its reverse
        int sum = rev + n;
     
        // Check for reverse number
        // reach digit must be odd
        while (sum > 0 && (rem % 2 != 0))
        {
            rem = sum % 10;
            sum /= 10;
        }
     
        if (sum == 0)
            System.out.println("Reversible Number");
        else
            System.out.println("Non-Reversible Number");
    }
     
    // Driver function
    public static void main (String[] args)
    {
        int n = 36;
        checkReversible(n);
         
    }
}
// This code is contributed by vt_m.


Python3
# Python3 program to check if a given
# number is reversible or not
 
# Function to check reversible number
def checkReversible (n):
     
    rev = 0
   
    # Calculate reverse of n
    flag = n
     
    while (flag != 0):
        rem = flag % 10
        rev *= 10
        rev += rem
        flag //= 10
     
    # Calculate sum of number
    # and its reverse
    sum = rev + n
   
    # Check for reverse number
    # reach digit must be odd
    while (sum and ((rem % 2) != 0)):
        rem = sum % 10
        sum //= 10
     
    if (sum == 0):
        print("Reversible Number")
    else:
        print("Non-Reversible Number")
 
# Driver Code
n = 36
 
checkReversible(n)
 
# This code is contributed by sanjoy_62


C#
// C# program to check if a given
// number is reversible or not
using System;
 
class GFG {
     
    // Function to check reversible number
    static void checkReversible (int n)
    {
        int rev = 0, rem = 0;
     
        // Calculate reverse of n
        int flag = n;
        while (flag > 0)
        {
            rem = flag % 10;
            rev *= 10;
            rev += rem;
            flag /= 10;
        }
     
        // Calculate sum of number
        // and its reverse
        int sum = rev + n;
     
        // Check for reverse number
        // reach digit must be odd
        while (sum > 0 && (rem % 2 != 0))
        {
            rem = sum % 10;
            sum /= 10;
        }
     
        if (sum == 0)
        Console.WriteLine("Reversible Number");
        else
        Console.WriteLine("Non-Reversible Number");
    }
     
    // Driver function
    public static void Main ()
    {
        int n = 36;
          checkReversible(n);
         
    }
}
 
// This code is contributed by vt_m.


C++
// C++ program to find the count of
// reversible numbers upto a given number n
#include 
#include 
using namespace std;
 
// Function to calculate the count of reversible number
void countReversible (int n)
{
    int count = 0;
 
    // Calculate counts of
    // reversible number of 1 to n-digits
    for ( int i = 1; i <= n; i++)
    {
        // All four possible cases and their formula
        switch (i % 4)
        {
 
        // for i of form 2k
        case 0:
        case 2:
            count += 20 * pow( 30, ( i / 2 - 1));
            break;
 
        // for i of form 4k + 3
        case 3:
            count += 100 * pow ( 500, i / 4 );
            break;
 
        // for i of form 4k + 1 no solution
        case 1:
            break;
        }
    }
    cout << count;
}
 
// Driver function
int main()
{
    // count up-to 9 digit numbers (1 billion)
    int n = 9;
    countReversible(n);
    return 0;
}


Java
// Java program to find the count of
// reversible numbers upto a given number n
import java.io.*;
 
class GFG {
 
    // Function to calculate the count
    // of reversible number
    static void countReversible (int n)
    {
        int count = 0;
     
        // Calculate counts of
        // reversible number of 1 to n-digits
        for ( int i = 1; i <= n; i++)
        {
            // All four possible cases
            // and their formula
            switch (i % 4)
            {
     
            // for i of form 2k
            case 0:
            case 2:
                count += 20 * Math.pow( 30, ( i / 2 - 1));
                break;
     
            // for i of form 4k + 3
            case 3:
                count += 100 * Math.pow ( 500, i / 4 );
                break;
     
            // for i of form 4k + 1 no solution
            case 1:
                break;
            }
        }
        System.out.println(count);
    }
     
    // Driver function
    public static void main (String[] args)
    {
        // count up-to 9 digit numbers (1 billion)
        int n = 9;
        countReversible(n);
         
    }
}
// This code is contributed by vt_m.


C#
// C# program to find the count of
// reversible numbers upto a given number n
using System;
 
class GFG {
     
    // Function to calculate the
    // count of reversible number
    static void countReversible (int n)
    {
        int count = 0;
     
        // Calculate counts of
        // reversible number of 1 to n-digits
        for ( int i = 1; i <= n; i++)
        {
            // All four possible cases
            // and their formula
            switch (i % 4)
            {
     
            // for i of form 2k
            case 0:
            case 2:
                count += 20 * (int)Math.Pow( 30, ( i / 2 - 1));
                break;
     
            // for i of form 4k + 3
            case 3:
                count += 100 * (int)Math.Pow ( 500, i / 4 );
                break;
     
            // for i of form 4k + 1 no solution
            case 1:
                break;
            }
        }
        Console.WriteLine(count);
    }
     
    // Driver function
    public static void Main ()
    {
        // count up-to 9 digit numbers (1 billion)
        int n = 9;
        countReversible(n);
         
    }
}
 
// This code is contributed by vt_m.


输出:

Reversible Number

程序计算最多n的可逆总数

C++

// C++ program to find the count of
// reversible numbers upto a given number n
#include 
#include 
using namespace std;
 
// Function to calculate the count of reversible number
void countReversible (int n)
{
    int count = 0;
 
    // Calculate counts of
    // reversible number of 1 to n-digits
    for ( int i = 1; i <= n; i++)
    {
        // All four possible cases and their formula
        switch (i % 4)
        {
 
        // for i of form 2k
        case 0:
        case 2:
            count += 20 * pow( 30, ( i / 2 - 1));
            break;
 
        // for i of form 4k + 3
        case 3:
            count += 100 * pow ( 500, i / 4 );
            break;
 
        // for i of form 4k + 1 no solution
        case 1:
            break;
        }
    }
    cout << count;
}
 
// Driver function
int main()
{
    // count up-to 9 digit numbers (1 billion)
    int n = 9;
    countReversible(n);
    return 0;
}

Java

// Java program to find the count of
// reversible numbers upto a given number n
import java.io.*;
 
class GFG {
 
    // Function to calculate the count
    // of reversible number
    static void countReversible (int n)
    {
        int count = 0;
     
        // Calculate counts of
        // reversible number of 1 to n-digits
        for ( int i = 1; i <= n; i++)
        {
            // All four possible cases
            // and their formula
            switch (i % 4)
            {
     
            // for i of form 2k
            case 0:
            case 2:
                count += 20 * Math.pow( 30, ( i / 2 - 1));
                break;
     
            // for i of form 4k + 3
            case 3:
                count += 100 * Math.pow ( 500, i / 4 );
                break;
     
            // for i of form 4k + 1 no solution
            case 1:
                break;
            }
        }
        System.out.println(count);
    }
     
    // Driver function
    public static void main (String[] args)
    {
        // count up-to 9 digit numbers (1 billion)
        int n = 9;
        countReversible(n);
         
    }
}
// This code is contributed by vt_m.

C#

// C# program to find the count of
// reversible numbers upto a given number n
using System;
 
class GFG {
     
    // Function to calculate the
    // count of reversible number
    static void countReversible (int n)
    {
        int count = 0;
     
        // Calculate counts of
        // reversible number of 1 to n-digits
        for ( int i = 1; i <= n; i++)
        {
            // All four possible cases
            // and their formula
            switch (i % 4)
            {
     
            // for i of form 2k
            case 0:
            case 2:
                count += 20 * (int)Math.Pow( 30, ( i / 2 - 1));
                break;
     
            // for i of form 4k + 3
            case 3:
                count += 100 * (int)Math.Pow ( 500, i / 4 );
                break;
     
            // for i of form 4k + 1 no solution
            case 1:
                break;
            }
        }
        Console.WriteLine(count);
    }
     
    // Driver function
    public static void Main ()
    {
        // count up-to 9 digit numbers (1 billion)
        int n = 9;
        countReversible(n);
         
    }
}
 
// This code is contributed by vt_m.

输出:

608720

时间复杂度O(n)
参考: Euler项目145:十亿以下有多少个可逆数字?