Hi guys,
I’ve encountered a strange bug.
For the problem “Johnny and the Beanstalk” (or A2), this code is graded as “correct”
#include <iostream>
#include <stdio.h>
using namespace std;
inline void read(int *input)
{
char ch = 0;
while(ch<33){
ch = getchar();
}
*input = 0;
while(ch > 33) {
*input = *input * 10 + ch - '0';
ch = getchar();
}
}
int main()
{
int case_num;
read(&case_num);
while(case_num){
case_num--;
int level_num;
read(&level_num);
float curr_stem_number = 1;
for (int i = 0; i < level_num; i++){
int leaf_num;
read(&leaf_num);
curr_stem_number = (curr_stem_number - leaf_num) * 2;
}
if (curr_stem_number == 0){
cout << "Yes" << endl;
}
else{
cout << "No" << endl;
}
}
}
The strange thing is, if I change the line 32 from
“float curr_stem_number = 1;
” to “int curr_stem_number = 1;
”,
the code is graded as “wrong answer”. This seems quite strange to me, since the curr_stem_number variable is initialized as an integer value 1, and the only actions applied to it are subtraction of another integer and multiplication by 2. Can anybody tell me what’s going on??
The code after the change:
#include <iostream>
#include <stdio.h>
using namespace std;
inline void read(int *input)
{
char ch = 0;
while(ch<33){
ch = getchar();
}
*input = 0;
while(ch > 33) {
*input = *input * 10 + ch - '0';
ch = getchar();
}
}
int main()
{
int case_num;
read(&case_num);
while(case_num){
case_num--;
int level_num;
read(&level_num);
int curr_stem_number = 1;
for (int i = 0; i < level_num; i++){
int leaf_num;
read(&leaf_num);
curr_stem_number = (curr_stem_number - leaf_num) * 2;
}
if (curr_stem_number == 0){
cout << "Yes" << endl;
}
else{
cout << "No" << endl;
}
}
}