给定一个整数X ,任务是计算具有差X的三位数的总数,其反数为X。如果不存在这样的数字,则打印-1。
例子:
Input: X = 792
Output : 10
Explanation :
901 – 109 = 792
911 – 119 = 792
921 – 129 = 792
931 – 139 = 792
941 – 149 = 792
951 – 159 = 792
961 – 169 = 792
971 – 179 = 792
981 – 189 = 792
991 – 199 = 792
Input: X = 0
Output: 90
方法:可以根据以下观察结果解决给定问题:
Let N = rpq
Therefore, N = 100r + 10q + p
Therefore, reverse of N = 100p + 10q + r
Therefore, the problem reduces to solving (100r + 10q + p) – (r + 10q + 100p) = X
-> 99(r – p) = X
-> r – p = X / 99
Therefore, if given X is a multiple of 99, then solution exists.
请根据以下观察结果,按照以下步骤解决问题:
- 检查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)