📜  计算与它的反面相差 X 的三位数

📅  最后修改于: 2021-10-26 05:43:54             🧑  作者: Mango

给定一个整数X ,任务是计算与它的反面相差X的三位数的总数。如果不存在这样的数字,则打印 -1。

例子:

方法:根据以下观察可以解决给定的问题:

根据以上观察,按照以下步骤解决问题:

  • 检查 X 是否是 99 的倍数。如果发现不正确,则打印 -1 作为不存在解决方案。
  • 否则,计算X / 99 。使用数字 [1, 9] 生成所有对,对于每一对,检查它们的差值是否等于X / 99
  • 如果发现任何对都为真,则将 count 增加 10,因为可以将中间数字置换为所获得对的 [0, 9] 范围内的任何值。
  • 最后,打印获得的计数值。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
 
#include 
using namespace std;
 
// Function to count three-digit
// numbers having difference x
// with its reverse
int Count_Number(int x)
{
    int ans = 0;
 
    // if x is not multiple of 99
    if (x % 99 != 0) {
 
        // No solution exists
        ans = -1;
    }
    else {
 
        int diff = x / 99;
 
        // Generate all possible pairs
        // of digits [1, 9]
        for (int i = 1; i < 10; i++) {
            for (int j = 1; j < 10; j++) {
 
                // If any pair is obtained
                // with difference x / 99
                if ((i - j) == diff) {
 
                    // Increase count
                    ans += 10;
                }
            }
        }
    }
 
    // Return the count
    return ans;
}
 
// Driver Code
int main()
{
    int x = 792;
    cout << Count_Number(x) << endl;
 
    return 0;
}


Java
// Java program to implement
// the above approach
import java.io.*;
import java.util.Arrays;
 
class GFG{
  
// Function to count three-digit
// numbers having difference x
// with its reverse
static int Count_Number(int x)
{
    int ans = 0;
   
    // If x is not multiple of 99
    if (x % 99 != 0)
    {
         
        // No solution exists
        ans = -1;
    }
    else
    {
        int diff = x / 99;
   
        // Generate all possible pairs
        // of digits [1, 9]
        for(int i = 1; i < 10; i++)
        {
            for(int j = 1; j < 10; j++)
            {
                 
                // If any pair is obtained
                // with difference x / 99
                if ((i - j) == diff)
                {
                     
                    // Increase count
                    ans += 10;
                }
            }
        }
    }
     
    // Return the count
    return ans;
}
  
// Driver Code
public static void main (String[] args)
{
    int x = 792;
     
    System.out.println(Count_Number(x));
}
}
 
// This code is contributed by sanjoy_62


Python3
# Python3 program to implement
# the above approach
 
# Function to count three-digit
# numbers having difference x
# with its reverse
def Count_Number(x):
   
    ans = 0;
 
    # If x is not multiple
    # of 99
    if (x % 99 != 0):
 
        # No solution exists
        ans = -1;
    else:
        diff = x / 99;
 
        # Generate all possible pairs
        # of digits [1, 9]
        for i in range(1, 10):
            for j in range(1, 10):
 
                # If any pair is obtained
                # with difference x / 99
                if ((i - j) == diff):
                    # Increase count
                    ans += 10;
 
    # Return the count
    return ans;
 
# Driver Code
if __name__ == '__main__':
   
    x = 792;
    print(Count_Number(x));
 
# This code is contributed by shikhasingrajput


C#
// C# program to implement
// the above approach 
using System;
 
class GFG{
  
// Function to count three-digit
// numbers having difference x
// with its reverse
static int Count_Number(int x)
{
    int ans = 0;
    
    // If x is not multiple of 99
    if (x % 99 != 0)
    {
         
        // No solution exists
        ans = -1;
    }
    else
    {
        int diff = x / 99;
    
        // Generate all possible pairs
        // of digits [1, 9]
        for(int i = 1; i < 10; i++)
        {
            for(int j = 1; j < 10; j++)
            {
                  
                // If any pair is obtained
                // with difference x / 99
                if ((i - j) == diff)
                {
                     
                    // Increase count
                    ans += 10;
                }
            }
        }
    }
      
    // Return the count
    return ans;
}
  
// Driver Code
public static void Main()
{
    int x = 792;
      
    Console.WriteLine(Count_Number(x));
}
}
 
// This code is contributed by code_hunt


Javascript


输出
10

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