24h購物| | PChome| 登入
2012-10-12 20:34:59| 人氣1,092| 回應0 | 上一篇 | 下一篇

[UVA][建表] 974 - Kaprekar Numbers

推薦 0 收藏 0 轉貼0 訂閱站台

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;
}

台長: Morris
人氣(1,092) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][數學] 10680 - LCM
此分類上一篇:[UVA][spfa] 10278 - Fire Station

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文