junkayden's blog

By junkayden, 7 years ago, In English

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;

}
  • Vote: I like it
  • -18
  • Vote: I do not like it

| Write comment?
»
7 years ago, # |
  Vote: I like it 0 Vote: I do not like it

notin is not initialized?

  • »
    »
    7 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    I initialized notin at first line in main function.(int n, need, notin)

    • »
      »
      »
      7 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      When you initialize a primitive variable inside the main, nothing assures you to be initialized with a specific value, in this case 0.

      • »
        »
        »
        »
        7 years ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

        Thank you for reply. But I found out that vector was causing problem. I changed vector to array, and it worked.