UVA974 Kaprekar Numbers【Kaprekar数】

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 102 = 100 and 10+0=10 (but the second part of the sum is zero — not positive).
    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 ≤ 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.
Sample Input
3
2 90
30 35
50 55
Sample Output
case #1
9
45
55

case #2
no kaprekar numbers

case #3
55

问题链接UVA974 Kaprekar Numbers
问题简述:(略)
问题分析
    Kaprekar数问题。先通过初始化给数组做过标记,标记是否为Kaprekar数。
    然后,按照给定的区间判定输出。
程序说明:(略)
参考链接:(略)
题记:(略)

AC的C++语言程序如下:

/* UVA974 Kaprekar Numbers */

#include <iostream>

using namespace std;

const int N = 40000;
int weight[] = {1000000000, 100000000, 10000000, 1000000, 100000, 10000, 1000, 100, 10};
bool knum[N + 1];

bool judge(int n)
{
    for(int i = 0; i < 9; i++) {
        int a = n * n / weight[i];
        int b = n * n % weight[i];
        if(a && b && a + b == n)
            return true;
    }
    return false;
}

void init()
{
    knum[0] = false;
    knum[1] = false;
    for(int i = 2; i <= N; i++)
        knum[i] = judge(i);
}

int main()
{
    init();

    int n, a, b;
    scanf("%d", &n);
    for(int k = 1; k <= n; k++) {
        scanf("%d%d", &a, &b);

        printf("case #%d\n", k);
        int cnt = 0;
        for(int i = a; i <= b; i++)
            if(knum[i]) {
                printf("%d\n", i);
                cnt++;
            }
        if(cnt == 0) printf("no kaprekar numbers\n");

        if( k < n) printf("\n");
    }

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值