# The results are not consistent in custom test and submit results (n=1)......

• ``````class Solution {
public:
int numSquares(int n) {
int number = 0;
cin >> n;
number = n;
int dp[10000] = { };
int stack[20000] = { };
int record[20000] = { };
for (int i = 0;i <= 20000;i++){
stack[i] = 0;
record[i] = 0;
dp[int(i/2)] = 0;
}
int pointer = 0;
stack[pointer] = number;
while (pointer >= 0) {
if (stack[pointer] == sqrt(stack[pointer]) * sqrt(stack[pointer])) {
dp[stack[pointer]] = 1;
record[pointer] = 0;
pointer = pointer - 1;
} else {
if (record[pointer] == 0) {
record[pointer] = int(sqrt(int(stack[pointer] / 2)));
}
if (record[pointer] <= sqrt(stack[pointer])) {
if (dp[stack[pointer] - record[pointer] * record[pointer]]!= 0) {
if (dp[stack[pointer]] == 0 || dp[stack[pointer]] > dp[stack[pointer] - record[pointer] * record[pointer]] + 1) {
dp[stack[pointer]] = dp[stack[pointer] - record[pointer] * record[pointer]] + 1;
}
record[pointer] = record[pointer] + 1;
} else {
stack[pointer + 1] = stack[pointer] - record[pointer] * record[pointer];
pointer = pointer + 1;
}
} else {
record[pointer] = 0;
pointer = pointer - 1;
}
}
}
return dp[number];
}
};``````

• What is `cin >> n;` supposed to do when `cin` is "empty"? Leave n alone or overwrite it with something (with what?)? Is it defined at all? I remember wondering about this before and I couldn't find out. Do you know it?

Anyway, if you `cout << n;` right afterwards, you'll find that in custom test with n=1, n is still 1, and in full submit with n=1, n somehow becomes 2. So there's your difference.

• Thanks for your answer. As you said, I delete the cin>>n;, but the result is not correct when n=11. In my local computer, the result is 3 instead of 1 on the oj. Why the results are not consistent again?

• You should add some print statements to see what's going on. Then you'd see it walks straight into the first `if` because `11 == sqrt(11) * sqrt(11)` is true. Don't know why it isn't on your PC. What C++ compiler are you using?

• Also, what does this print on your PC?

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

int main() {
auto x = sqrt(11);
auto i = sqrt(int(11));
auto f = sqrt(float(11));
auto d = sqrt(double(11));
cout << (x * x == 11) << endl;
cout << (i * i == 11) << endl;
cout << (f * f == 11) << endl;
cout << (d * d == 11) << endl;
cout << typeid(x).name() << endl;
}
``````

• g++ -O0 -g3 -Wall -c -fmessage-length=0 -otest.o ..\test.cpp
error: ISO C++ forbids declaration of `x' with no type warning: converting to`int' from `double' error: ISO C++ forbids declaration of`i' with no type
warning: converting to `int' from`double'
error: ISO C++ forbids declaration of `f' with no type warning: converting to`int' from `float' error: ISO C++ forbids declaration of`d' with no type
warning: converting to `int' from`double'

cannot be compiled.

In my computer, 11==sqrt(11)*sqrt(11) is false and I think the precision is not the same. I am afraid I don't know how to see the c++ compiler version. I use the eclipse 3.6.1.

• You can probably compile it by adding `-std=c++11`.
If you try again, copy the code again as I just fixed something.

But I tried on my PC and get the same behavior as you on yours, I think. Something odd is going on. I might ask a stackoverflow question.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.