📜  计算具有X倒数的三位数

📅  最后修改于: 2021-04-29 08:35:45             🧑  作者: Mango

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

例子:

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

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

  • 检查X是否为99的倍数。如果发现不是真的,则打印-1,因为不存在任何解决方案。
  • 否则,计算X / 99 。使用数字[1,9]生成所有对,并为每对检查它们的差是否等于X / 99
  • 如果发现对于任何一对都是正确的,则将计数增加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


输出
10

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