My code is working with my Xcode and gnu(g++) compiler, but codeforces gnu g++14 compiler is keep showing me runtime error. I can't understand why.
exit code is: "-1073781419"
This is my code-
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main() {
int n, need, notin;
vector<string> snacks,ans;
cin >> n;
for(int i = 1; i <= n; i++) {
string size;
cin >> size;
snacks.push_back(size);
}
need = n;
for(int i = 0; i < snacks.size(); i++) {
if(snacks[i] == to_string(need)) {
if(notin != 0) {
for(int j = notin; j != 0; j--) {
snacks[i] += " " + snacks[i-j];
notin--;
need--;
}
}
ans.push_back(snacks[i]);
need--;
} else {
notin++;
ans.push_back("0");
}
}
for(int i = 0; i < ans.size(); i++) {
if(ans[i] == "0") {
cout << endl;
} else {
cout << ans[i] << endl;
}
}
return 0;
}
notin is not initialized?
I initialized notin at first line in main function.(int n, need, notin)
When you initialize a primitive variable inside the main, nothing assures you to be initialized with a specific value, in this case 0.
Thank you for reply. But I found out that vector was causing problem. I changed vector to array, and it worked.