2012-10-12 20:34:59Morris

[UVA][建表] 974 - Kaprekar Numbers

Problem C - Kaprekar Numbers

Shri Dattathreya Ramachandra Kaprekar was an Indian mathematician, whose name is associated with a number of concepts in number theory. He was born in Dahanu, near Mumbai, in India. Even as a small child, he was already interested in numbers.

Among his numerous contributions are the so called Kaprekar numbers. These are non-negative integer numbers such that the representation of their square can be split into two parts that add up to the original number again. For example, 55 is a Kaprekar number, because 552 = 3025, which can be split into 30 and 25, and 30 + 25 = 55. There is one special rule: both parts of this sum must be positive. This means that, for example, 10 is not a Kaprekar number, even when 10^2 = 100 and 10+0=10 (but the second part of the sum is zero - not positive).

The Problem

Given a range of numbers, your task is to discover and print all Kaprekar numbers within that range.

Input

The first line of input contains a single number N, representing the number of test cases that follow (1 ≤ N ≤ 1000).

Than follow exactly N lines (one for each test case), each one containing two positive integers separated by a single space: INF and SUP (2 ≤ INF ≤ SUP &le 40000), indicating that the range to consider is the number interval [INF,SUP] (this means that the limits are included in the interval to consider).

Output

For each test case you should start by printing a line in the format case #NUM where NUM is the test case number (starting in one).

Then, you must print all the Kaprekar numbers that appear in the respective range, in ascending order, one per line. If there are no Kaprekar numbers in the specified interval, you should print no kaprekar numbers.

There should also be a blank line between test cases.

See the sample output for an example.

Example Input

3
2 90
30 35
50 55

Example Output

case #1
9
45
55

case #2
no kaprekar numbers

case #3
55

2006 Programming Contest of Porto University
Round 3, 11th of October of 2006
 
(Author: Pedro Ribeiro - DCC/FCUP)




#include <stdio.h>

int main() {
    int i, j, x, y, tmp;
    char buf[30];
    int ans[50], at = 0;
    for(i = 4; i <= 40000; i++) {
        j = i*i;
        sprintf(buf, "%d", j);
        for(j = 0; buf[j+1]; j++) {
            tmp = buf[j+1];
            buf[j+1] = '\0';
            sscanf(buf, "%d", &x);
            buf[j+1] = tmp;
            sscanf(buf+j+1, "%d", &y);
            if(x && y && x+y == i) {
                ans[at++] = i;
                break;
            }
        }
    }
    int cases = 0;
    scanf("%d", &tmp);
    while(tmp--) {
        scanf("%d %d", &x, &y);
        printf("case #%d\n", ++cases);
        for(i = 0, j = 0; i < at; i++)
            if(ans[i] >= x && ans[i] <= y)
                printf("%d\n", ans[i]), j = 1;
        if(!j)
            puts("no kaprekar numbers");
        if(tmp) puts("");
    }
    return 0;
}