给定一个数字N ,任务是打印前N个数字,以使每个数字和该数字的倒数都可以被其数字和除。
例子:
Input: N = 4
Output: 1 2 3 4
Explanation:
The reverse of every single digit number is the same number. And, every number is divisible by itself.
Input: N = 12
Output: 1 2 3 4 5 6 7 8 9 10 12 18
方法:想法是迭代从1开始的每个数字,并计算数字的总和。对于每个这样的数字,请检查该数字和该数字的反数是否可被和除。因此,请按照以下步骤计算答案:
- 将计数器初始化为1,并一一遍历所有数字。
- 对于每个数字,找到该数字的反面。
- 在找到数字的倒数时,请计算数字的总和。
- 现在,检查数字和数字的反向是否可被其数字的总和整除。
- 如果是,则增加计数器。重复上述步骤,直到该计数器等于N为止。
下面是上述方法的实现:
C++
// C++ program to print the first N numbers
// such that every number and the reverse
// of the number is divisible by its
// sum of digits
#include
using namespace std;
// Function to calculate the sum of digits
int digit_sum(int n)
{
int sum = 0, m;
// Loop to iterate through every
// digit of the number
while (n > 0) {
m = n % 10;
sum = sum + m;
n = n / 10;
}
// Returning the sum of digits
return (sum);
}
// Function to calculate the reverse
// of a number
int reverse(int n)
{
int r = 0;
// Loop to calculate the reverse
// of the number
while (n != 0) {
r = r * 10;
r = r + n % 10;
n = n / 10;
}
// Return the reverse of the
// number
return (r);
}
// Function to print the first N numbers
// such that every number and the reverse
// of the number is divisible by its
// sum of digits
void operation(int n)
{
int i = 1, a, count = 0, r;
// Loop to continuously check and
// generate number until there
// are n outputs
while (count < n) {
// Variable to hold the sum of
// the digit of the number
a = digit_sum(i);
// Computing the reverse of the
// number
r = reverse(i);
// Checking if the condition satisfies.
// Increment the count and print the
// number if it satisfies.
if (i % a == 0 && r % a == 0) {
cout << i << " ";
count++;
i++;
}
else
i++;
}
}
// Driver code
int main()
{
int n = 10;
operation(n);
}
Java
// Java program to print the first N numbers
// such that every number and the reverse
// of the number is divisible by its
// sum of digits
import java.util.*;
class GFG{
// Function to calculate the sum of digits
static int digit_sum(int n)
{
int sum = 0, m;
// Loop to iterate through
// every digit of the number
while (n > 0)
{
m = n % 10;
sum = sum + m;
n = n / 10;
}
// Returning the sum of digits
return (sum);
}
// Function to calculate the
// reverse of a number
static int reverse(int n)
{
int r = 0;
// Loop to calculate the
// reverse of the number
while (n != 0)
{
r = r * 10;
r = r + n % 10;
n = n / 10;
}
// Return the reverse
// of the number
return (r);
}
// Function to print the first N numbers
// such that every number and the reverse
// of the number is divisible by its
// sum of digits
static void operation(int n)
{
int i = 1, a, count = 0, r;
// Loop to continuously check and
// generate number until there
// are n outputs
while (count < n)
{
// Variable to hold the sum
// of the digit of the number
a = digit_sum(i);
// Computing the reverse of the
// number
r = reverse(i);
// Checking if the condition satisfies.
// Increment the count and print the
// number if it satisfies.
if (i % a == 0 && r % a == 0)
{
System.out.print(i + " ");
count++;
i++;
}
else
i++;
}
}
// Driver code
public static void main(String args[])
{
int n = 10;
operation(n);
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python3 program to print the first N numbers
# such that every number and the reverse
# of the number is divisible by its
# sum of digits
# Function to calculate the sum of digits
def digit_sum(n):
sum = 0
# Loop to iterate through every
# digit of the number
while (n > 0):
m = n % 10;
sum = sum + m;
n = n //10
# Returning the sum of digits
return (sum)
# Function to calculate the reverse
# of a number
def reverse(n):
r = 0
# Loop to calculate the reverse
# of the number
while (n != 0):
r = r * 10
r = r + n % 10
n = n // 10
# Return the reverse of the
# number
return (r)
# Function to print the first N numbers
# such that every number and the reverse
# of the number is divisible by its
# sum of digits
def operation(n):
i = 1
count = 0
# Loop to continuously check and
# generate number until there
# are n outputs
while (count < n):
# Variable to hold the sum of
# the digit of the number
a = digit_sum(i)
# Computing the reverse of the
# number
r = reverse(i)
# Checking if the condition satisfies.
# Increment the count and print the
# number if it satisfies.
if (i % a == 0 and r % a == 0):
print(i, end = " ")
count += 1
i += 1
else:
i += 1
# Driver code
if __name__ == '__main__':
n = 10
operation(n)
# This code is contributed by Samarth
C#
// C# program to print the first N numbers
// such that every number and the reverse
// of the number is divisible by its
// sum of digits
using System;
class GFG{
// Function to calculate the sum of digits
static int digit_sum(int n)
{
int sum = 0, m;
// Loop to iterate through
// every digit of the number
while (n > 0)
{
m = n % 10;
sum = sum + m;
n = n / 10;
}
// Returning the sum of digits
return (sum);
}
// Function to calculate the
// reverse of a number
static int reverse(int n)
{
int r = 0;
// Loop to calculate the
// reverse of the number
while (n != 0)
{
r = r * 10;
r = r + n % 10;
n = n / 10;
}
// Return the reverse
// of the number
return (r);
}
// Function to print the first N numbers
// such that every number and the reverse
// of the number is divisible by its
// sum of digits
static void operation(int n)
{
int i = 1, a, count = 0, r;
// Loop to continuously check and
// generate number until there
// are n outputs
while (count < n)
{
// Variable to hold the sum
// of the digit of the number
a = digit_sum(i);
// Computing the reverse of the
// number
r = reverse(i);
// Checking if the condition satisfies.
// Increment the count and print the
// number if it satisfies.
if (i % a == 0 && r % a == 0)
{
Console.Write(i + " ");
count++;
i++;
}
else
i++;
}
}
// Driver code
public static void Main()
{
int n = 10;
operation(n);
}
}
// This code is contributed by Code_Mech
Javascript
输出:
1 2 3 4 5 6 7 8 9 10