Click here to Skip to main content
16,015,583 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Given a natural number k (1≤k≤18), we need to find the smallest square number with k digits, where each digit is also a square number.

Input:

The number of test cases t (0<t<10^3).
For each test case, input a natural number k.
Output:

Each line corresponds to a test case and prints the found number. If no number is found, print -1.
Example:
Input:
2
1
3

Output:
1
100


What I have tried:

#include<iostream>
#include<cmath>
using namespace std;

int scp(int n){
    int can = sqrt(n);
    if (can * can == n)
        return 1;
    else
        return 0;
}

long long find(int k){
    if (k > 18){
        return -1;
    }

    long long socantim = 0;
    for (int i = 0; i < k; i++){
        int khoitao = 1;
        while (!scp(khoitao)){
            khoitao++;
        }
        socantim = socantim * 10 + khoitao;
    }

    if (socantim == 0){
        return -1;
    }

    return socantim;
}

int main(){
    int t;
    cin >> t;
    while (t--){
        int a;
        cin >> a;
        cout << find(a) << endl;
    }
    return 0;
}
Posted
Updated 18-Jul-23 0:54am
Comments
Patrice T 18-Jul-23 3:57am    
What is the problem with your code ?
B20DCVT339-Trần Anh Tuấn 18-Jul-23 4:05am    
i think i thought wrong algorithm
B20DCVT339-Trần Anh Tuấn 18-Jul-23 4:05am    
my code is not running properly testcase
Patrice T 18-Jul-23 4:21am    
Use Improve question to update your question.
So that everyone can pay attention to this information. anf give details.
Andre Oosthuizen 18-Jul-23 4:22am    
You need to be more specific, "not running properly" does not tell us anything. Where does it error, what is the error code? What output are getting now compared to what is expected?

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

Start by reading the assignment carefully, and working out what you need to know - then try to do it manually. Remember that "each digit is also a square number", and that an 18 digit number won't fit in an int at all ...

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 
Hint 1: if k is odd then 10k-1 is the solution.
Then, for even values of k:
Hint 2: you need a 64-bit integer to hold number having up to 18 digits.
Hint 3: you may solve it by construction, generating the squares.
 
Share this answer
 
Comments
Patrice T 18-Jul-23 18:26pm    
+5
CPallini 19-Jul-23 0:34am    
Thank you.
Input:
2
1
3

Output:
1
100

I fear the assignment is wrong.
I don't see any reason why 0 is not the answer for k=1.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900