c++
-
#include
using namespace std;bool checkSquareNumber(int num)
{
int i=0;
while(i*i <= num)
{
if(i*i==num)
return true;
i++;
}
return false;
}
int squareNumber(int num)
{
int i=0;
while(i*i <= num)
{
if(i*i==num)
return i;
i++;
}
return i;
}
int numberOfStep(int num)
{
int cnt =0;
int i =0;
while(num)
{
if(checkSquareNumber(num)== true)
{
num = squareNumber(num);
cnt++;
}
else if(num%3==0)
{
num = num/3;
cnt++;
}
else if(num%2==0)
{
num/=2;
cnt++;
}
else
{
num--;
cnt++;
}
}
return cnt;
}
int main()
{
int num,t;
cin>>t;
while(t--)
{
cin>>num;
cout< -
#include
using namespace std;bool checkSquareNumber(int num)
{
int i=0;
while(i*i <= num)
{
if(i*i==num)
return true;
i++;
}
return false;
}
int squareNumber(int num)
{
int i=0;
while(i*i <= num)
{
if(i*i==num)
return i;
i++;
}
return i;
}
int numberOfStep(int num)
{
int cnt =0;
int i =0;
while(num)
{
if(checkSquareNumber(num)== true)
{
num = squareNumber(num);
cnt++;
}
else if(num%3==0)
{
num = num/3;
cnt++;
}
else if(num%2==0)
{
num/=2;
cnt++;
}
else
{
num--;
cnt++;
}
}
return cnt;
}
int main()
{
int num,t;
cin>>t;
while(t--)
{
cin>>num;
cout<Quote:
I'm having a problem with my code. can anyone point out where my error is?. i am trying to count the minimum number of steps to get any number to 0
-
Quote:
I'm having a problem with my code. can anyone point out where my error is?. i am trying to count the minimum number of steps to get any number to 0
Ok, so it appears the rules of this game are: - If a number is a perfect square, take its square root - if a number is divisible by 3, divide it by 3 - if a number is divisible by 2, divide it by 2 - otherwise, subtract 1. So, given an input of 32, we'd go 32 -> 16 -> 4 -> 2 -> 1 -> 0 for 5 steps. and given 75, we'd go 75 -> 25 -> 5 -> 4 -> 2 -> 1 -> 0 for 6 steps. Now, what problem are you having? A quick glance over the code didn't show any obvious errors. Your checkSquareNumber() and squareNumber() functions are quite inefficient. You'd be better off googling a better square root algorithm (or just using the library sqrt() function). Ad since they are doing the same thing, they could be combined (return -1 if it is not a perfect square, and you can replace the two calls with one)
Truth, James
-
Ok, so it appears the rules of this game are: - If a number is a perfect square, take its square root - if a number is divisible by 3, divide it by 3 - if a number is divisible by 2, divide it by 2 - otherwise, subtract 1. So, given an input of 32, we'd go 32 -> 16 -> 4 -> 2 -> 1 -> 0 for 5 steps. and given 75, we'd go 75 -> 25 -> 5 -> 4 -> 2 -> 1 -> 0 for 6 steps. Now, what problem are you having? A quick glance over the code didn't show any obvious errors. Your checkSquareNumber() and squareNumber() functions are quite inefficient. You'd be better off googling a better square root algorithm (or just using the library sqrt() function). Ad since they are doing the same thing, they could be combined (return -1 if it is not a perfect square, and you can replace the two calls with one)
Truth, James
Quote:
i am having problem with my code. where the first condition to check if the original number is a perfect square. can you point out where my error is or point me to a better direction for this problem. i am trying to count the minimum number of steps to convert any number to 0
-
Quote:
i am having problem with my code. where the first condition to check if the original number is a perfect square. can you point out where my error is or point me to a better direction for this problem. i am trying to count the minimum number of steps to convert any number to 0
If you are not happy with the output produced by your code, then you should post here an example of input data (i.e. the sarting value of
num
), together withe the expected result. Otherwise, how could we possibly help?"In testa che avete, Signor di Ceprano?" -- Rigoletto
-
If you are not happy with the output produced by your code, then you should post here an example of input data (i.e. the sarting value of
num
), together withe the expected result. Otherwise, how could we possibly help?"In testa che avete, Signor di Ceprano?" -- Rigoletto
intput: 2 10 32 output: 4 5
-
If you are not happy with the output produced by your code, then you should post here an example of input data (i.e. the sarting value of
num
), together withe the expected result. Otherwise, how could we possibly help?"In testa che avete, Signor di Ceprano?" -- Rigoletto
Quote:
the problem is I can't run my code
-
Quote:
the problem is I can't run my code
A quick run with the debugger would spot immediately the bug: if
num
becomes1
then your code end in a infinite loop, because1
is a perfect square of itself. For a quick fix, replaceQuote:
if(checkSquareNumber(num)== true)
with
if(num > 1 && checkSquareNumber(num)== true)
"In testa che avete, Signor di Ceprano?" -- Rigoletto
-
Quote:
the problem is I can't run my code