Posted on

Description

Submission

  • (0, 100,000) => (0, 10,000,000,000): should use long type
  • from float to integer
  • The length of an integer: trunc(log10(s)) + 1
#include <bits/stdc++.h>

using namespace std;

// Complete the kaprekarNumbers function below.
void kaprekarNumbers(int p, int q) {
    bool exist = false;
    for(long i = p; i <= q; ++i) {
        long s = i * i;
        long n = trunc(log10(s)) + 1;
        long h = n / 2;
        if(n != h * 2) h++;
        long mask = floor(pow(10, h) + 0.5);
        long a = s / mask;
        long b = s - a *  mask;

        if(a + b == i) {
            cout << i << " ";
            exist = true;
        }
    }
    if(!exist) cout << "INVALID RANGE\n";
}

int main()
{
    int p;
    cin >> p;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    int q;
    cin >> q;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');

    kaprekarNumbers(p, q);

    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *