P15508 [HackerRank] Kaprekar numbers
普及-
题目描述

A modified Kaprekar number is a positive whole number with a special property. If you square it, then split the number into two integers and sum those integers, you have the same value you started with.

Consider a positive whole number $n$ with $\boldsymbol{d}$ digits. We square $n$ to arrive at a number that is either $2\times d$ digits long or $(2\times d)-1$ digits long. Split the string representation of the square into two parts, $\boldsymbol{l}$ and $\textbf{r}$. The right hand part, $\textbf{r}$ must be $\boldsymbol{d}$ digits long. The left is the remaining substring. Convert those two substrings back to integers, add them and see if you get $n$.

Example

$n=5$

$\boldsymbol{d}=1$

First calculate that $n^2=25$. Split that into two strings and convert them back to integers $2$ and $5$. Test $2+5=7\neq5$, so this is not a modified Kaprekar number. If $n=9$, still $\boldsymbol{d}=1$, and $n^2=81$. This gives us $1+8=9$, the original $n$.

Note: r may have leading zeros.

Here's an explanation from Wikipedia about the ORIGINAL Kaprekar Number (spot the difference!):

In mathematics, a Kaprekar number for a given base is a non-negative integer, the representation of whose square in that base can be split into two parts that add up to the original number again. For instance, 45 is a Kaprekar number, because 45² = 2025 and 20+25 = 45.

Given two positive integers $\boldsymbol{p}$ and $\textit{q}$ where $\boldsymbol{p}$ is lower than $\textit{q}$, write a program to print the modified Kaprekar numbers in the range between $\boldsymbol{p}$ and $\textit{q}$, inclusive. If no modified Kaprekar numbers exist in the given range, print INVALID RANGE.

Function Description

Complete the kaprekarNumbers function in the editor below.

kaprekarNumbers has the following parameter(s):

int p: the lower limit
int q: the upper limit

Prints

It should print the list of modified Kaprekar numbers, space-separated on one line and in ascending order. If no modified Kaprekar numbers exist in the given range, print INVALID RANGE. No return value is required.

Input Format

The first line contains the lower integer limit $\boldsymbol{p}$.

The second line contains the upper integer limit $\textit{q}$.

Note: Your range should be inclusive of the limits.

Constraints

$0<p<q<100000$

Sample Input
STDIN Function
----- --------
1 p = 1
100 q = 100

Sample Output

1 9 45 55 99

Explanation

$\mbox{1}$, $\mbox{9}$, $45$, $55$, and $\mbox{99}$ are the modified Kaprekar Numbers in the given range.

输入格式

见题面

输出格式

见题面

样例输入
1 100
样例输出
1 9 45 55 99