prithivMLmods/Coder-Stat huggingface dataset:
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, cnt;
  vector<int> nums;
  auto insertionSort = [&nums, &n, &cnt](int d) {
    for (int i = d; i < n; ++i) {
      int top = nums[i];
      int j;
      for (j = i - d; i >= 0 && nums[j] > top; j -= d) {
        nums[j + d] = nums[j];
        ++cnt;
      }
      nums[j + d] = top;
    }
  };
  auto shellSort = [&nums, &n, insertionSort]() {
    int maxid = log(2 * n + 1) / log(3) - 1;
    cout << maxid + 1 << endl;
    for (int id = maxid; id >= 0; --id) {
      unsigned int d = (pow(3, id + 1) - 1) / 2;
      cout << d << (id == 0 ? "\n" : " ");
      insertionSort(d);
    }
  };
  cin >> n;
  nums = vector<int>(n);
  for (int &c : nums)
    cin >> c;
  cnt = 0;
  shellSort();
  cout << cnt << endl;
  for (auto &&c : nums)
    cout << c << endl;
  return 0;
}

Fixed submission:
#include <cmath>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, cnt;
  vector<int> nums;
  auto insertionSort = [&nums, &n, &cnt](int d) {
    for (int i = d; i < n; ++i) {
      int top = nums[i];
      int j;
      for (j = i - d; j >= 0 && nums[j] > top; j -= d) {
        nums[j + d] = nums[j];
        ++cnt;
      }
      nums[j + d] = top;
    }
  };
  auto shellSort = [&nums, &n, insertionSort]() {
    int maxid = log(2 * n + 1) / log(3) - 1;
    cout << maxid + 1 << endl;
    for (int id = maxid; id >= 0; --id) {
      unsigned int d = (pow(3, id + 1) - 1) / 2;
      cout << d << (id == 0 ? "\n" : " ");
      insertionSort(d);
    }
  };
  cin >> n;
  nums = vector<int>(n);
  for (int &c : nums)
    cin >> c;
  cnt = 0;
  shellSort();
  cout << cnt << endl;
  for (auto &&c : nums)
    cout << c << endl;
  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int m;
vector<int> G;
int insertionSort(int A[], int n, int g) {
  int cnt = 0;
  for (int i = g; i < n; ++i) {
    int v = A[i];
    int j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j -= g;
      cnt++;
    }
    A[j + g] = v;
  }
  return cnt;
}
int shellSort(int A[], int N) {
  int cnt = 0;
  int x = 3;
  int y = 1;
  while (y <= N) {
    G.push_back(y);
    x *= 3;
    y = (x - 1) / 2;
  }
  reverse(G.begin(), G.end());
  m = G.size();
  for (int i = 0; i < m; ++i) {
    cnt += insertionSort(A, N, G[i]);
  }
  return cnt;
}
int main() {
  int N;
  cin >> N;
  int A[114514];
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  int res = shellSort(A, N);
  cout << m << endl;
  for (int i = 0; i < m; ++i) {
    if (i)
      cout << " ";
    cout << G[i];
    if (i == m - 1)
      cout << endl;
  }
  cout << res << endl;
  for (int i = 0; i < N; ++i) {
    cout << A[i] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int m;
vector<int> G;
int insertionSort(int A[], int n, int g) {
  int cnt = 0;
  for (int i = g; i < n; ++i) {
    int v = A[i];
    int j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j -= g;
      cnt++;
    }
    A[j + g] = v;
  }
  return cnt;
}
int shellSort(int A[], int N) {
  int cnt = 0;
  int x = 3;
  int y = 1;
  while (y <= N) {
    G.push_back(y);
    x *= 3;
    y = (x - 1) / 2;
  }
  reverse(G.begin(), G.end());
  m = G.size();
  for (int i = 0; i < m; ++i) {
    cnt += insertionSort(A, N, G[i]);
  }
  return cnt;
}
int main() {
  int N;
  cin >> N;
  int A[1145149];
  for (int i = 0; i < N; ++i)
    cin >> A[i];
  int res = shellSort(A, N);
  cout << m << endl;
  for (int i = 0; i < m; ++i) {
    if (i)
      cout << " ";
    cout << G[i];
    if (i == m - 1)
      cout << endl;
  }
  cout << res << endl;
  for (int i = 0; i < N; ++i) {
    cout << A[i] << endl;
  }
  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <list>
#include <string>
#include <vector>

using namespace std;

template <typename T> void WriteLine_Container(const T &container);
void InsertionSort(vector<int> &v, int n, int g, int &cnt);
void ShellSort(vector<int> &v, int n);

int main() {
  int len;
  cin >> len;
  vector<int> A(len);
  for (int i = 0; i < len; i++)
    cin >> A[i];

  ShellSort(A, A.size());

  for_each(A.begin(), A.end(), [](int x) { cout << x << endl; });

  return 0;
}

template <typename T> void WriteLine_Container(T &container) {

  auto it = container.begin();
  while (true) {
    cout << *it;
    it++;
    if (it != container.end())
      cout << " ";
  }
  cout << endl;
}

void InsertionSort(vector<int> &v, int n, int g, int &cnt) {
  for (int i = g; i < n; i++) {
    int temp = v[i];
    int j = i - g;
    while (j >= 0 && v[j] > temp) {
      v[j + g] = v[j];
      j = j - g;
      cnt++;
    }
    v[j + g] = temp;
  }
}

void ShellSort(vector<int> &v, int n) {
  int cnt = 0;

  list<int> li;
  li.push_front(1);

  for (int i = 4; i < v.size(); i = i * 3 + 1) {
    li.push_front(i);
  }
  int m = li.size();

  cout << m << endl;

  WriteLine_Container(li);

  for_each(li.begin(), li.end(), [&](int x) { InsertionSort(v, n, x, cnt); });

  cout << cnt << endl;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <list>
#include <string>
#include <vector>

using namespace std;

template <typename T> void WriteLine_Container(const T &container);
void InsertionSort(vector<int> &v, int n, int g, int &cnt);
void ShellSort(vector<int> &v, int n);

int main() {
  int len;
  cin >> len;
  vector<int> A(len);
  for (int i = 0; i < len; i++)
    cin >> A[i];

  ShellSort(A, A.size());

  for_each(A.begin(), A.end(), [](int x) { cout << x << endl; });

  return 0;
}

template <typename T> void WriteLine_Container(T &container) {

  auto it = container.begin();
  while (true) {
    cout << *it;
    it++;
    if (it == container.end())
      break;
    else
      cout << " ";
  }
  cout << endl;
}

void InsertionSort(vector<int> &v, int n, int g, int &cnt) {
  for (int i = g; i < n; i++) {
    int temp = v[i];
    int j = i - g;
    while (j >= 0 && v[j] > temp) {
      v[j + g] = v[j];
      j = j - g;
      cnt++;
    }
    v[j + g] = temp;
  }
}

void ShellSort(vector<int> &v, int n) {
  int cnt = 0;

  list<int> li;
  li.push_front(1);

  for (int i = 4; i < v.size(); i = i * 3 + 1) {
    li.push_front(i);
  }
  int m = li.size();

  cout << m << endl;

  WriteLine_Container(li);

  for_each(li.begin(), li.end(), [&](int x) { InsertionSort(v, n, x, cnt); });

  cout << cnt << endl;
}
Problem p02262 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <utility>

using namespace std;

void output(int n, int elements[]) {
  for (int i = 0; i < n; i++)
    cout << elements[i] << '\n';
}

int insertion_sort(int n, int elements[], int g) {
  int cnt = 0;
  for (int i = g; i < n; i++) {
    int key = elements[i], j = i - g;
    while (0 <= j && key < elements[j]) {
      elements[j + g] = elements[j];
      j -= g;
      cnt++;
    }
    elements[j + g] = key;
  }
  return cnt;
}

void shell_sort(int n, int elements[]) {
  int cnt = 0, m = 0, g[13] = {1,    4,    13,    40,    121,    364,   1093,
                               3280, 9841, 29524, 88573, 265720, 797161};

  for (; g[m] <= n; m++)
    ;
  cout << m << '\n';
  for (int i = m - 1; 0 <= i; i--) {
    cout << g[i] << (i == 0 ? "" : " ");
    cnt += insertion_sort(n, elements, g[i]);
  }
  cout << '\n';
  cout << cnt << '\n';
}

int main() {
  std::ios::sync_with_stdio(false);

  int n;
  cin >> n;
  int *elements = new int[n];

  for (int i = 0; i < n; i++)
    cin >> elements[i];

  shell_sort(n, elements);
  output(n, elements);
  delete[] elements;
  return 0;
}

Fixed submission:
#include <iostream>
#include <utility>

using namespace std;

void output(int n, int elements[]) {
  for (int i = 0; i < n; i++)
    cout << elements[i] << '\n';
}

int insertion_sort(int n, int elements[], int g) {
  int cnt = 0;
  for (int i = g; i < n; i++) {
    int key = elements[i], j = i - g;
    while (0 <= j && key < elements[j]) {
      elements[j + g] = elements[j];
      j -= g;
      cnt++;
    }
    elements[j + g] = key;
  }
  return cnt;
}

void shell_sort(int n, int elements[]) {
  int cnt = 0, m = 0, g[13] = {1,    4,    13,    40,    121,    364,   1093,
                               3280, 9841, 29524, 88573, 265720, 797161};

  for (; g[m] <= n && m < 13; m++)
    ;
  cout << m << '\n';
  for (int i = m - 1; 0 <= i; i--) {
    cout << g[i] << (i == 0 ? "" : " ");
    cnt += insertion_sort(n, elements, g[i]);
  }
  cout << '\n';
  cout << cnt << '\n';
}

int main() {
  std::ios::sync_with_stdio(false);

  int n;
  cin >> n;
  int *elements = new int[n];

  for (int i = 0; i < n; i++)
    cin >> elements[i];

  shell_sort(n, elements);
  output(n, elements);
  delete[] elements;
  return 0;
}
Problem p02262 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
vector<int> A;
int cnt;
void insertionSort(int n, int g) {
  int j, v;
  for (int i = g; i < n; i++) {
    v = A[i];
    j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j = j - g;
      cnt++;
    }
    A[j + g] = v;
  }
}
void shellSort(int n) {
  cnt = 0;
  int siz = 0;
  vector<int> G;
  while (1) {
    siz = siz * 3 + 1;
    if (siz >= n)
      break;
    G.push_back(siz);
  }
  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    insertionSort(n, G[i]);
    cout << G[i] << ((i != 0) ? " " : "\n");
  }
}

int main(void) {
  int n, t;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> t;
    A.push_back(t);
  }
  shellSort(n);
  cout << cnt << endl;
  for (int i : A) {
    cout << i << endl;
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
vector<int> A;
int cnt;
void insertionSort(int n, int g) {
  int j, v;
  for (int i = g; i < n; i++) {
    v = A[i];
    j = i - g;
    while (j >= 0 && A[j] > v) {
      A[j + g] = A[j];
      j = j - g;
      cnt++;
    }
    A[j + g] = v;
  }
}
void shellSort(int n) {
  cnt = 0;
  int siz = 0;
  vector<int> G;
  while (1) {
    siz = siz * 3 + 1;
    if (siz >= n && siz != 1)
      break;
    G.push_back(siz);
  }
  cout << G.size() << endl;
  for (int i = G.size() - 1; i >= 0; i--) {
    insertionSort(n, G[i]);
    cout << G[i] << ((i != 0) ? " " : "\n");
  }
}

int main(void) {
  int n, t;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> t;
    A.push_back(t);
  }
  shellSort(n);
  cout << cnt << endl;
  for (int i : A) {
    cout << i << endl;
  }
}
Problem p02263 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
#define MAX 100
#define max 1000000

void initialize();
int isEmpty();
int isFull();
void push(int);
int pop();

int top;
int S[max];

int main() {
  char c[max];
  int sum = 0;
  int a, b;

  initialize();

  while (cin >> c) {
    if (c[0] == '-') {
      a = pop();
      b = pop();
      push(b - a);
    } else if (c[0] == '+') {
      push(pop() + pop());
    } else if (c[0] == '*') {
      push(pop() * pop());
    } else {
      push(atoi(c));
    }
  }

  printf("%d\n", S[1]);

  return 0;
}

void initialize() { top = 0; }

int isEmpty() {
  if (top == 0)
    return 1;
  return 0;
}

int isFull() {
  if (top >= MAX - 1)
    return 1;
  return 0;
}

void push(int x) {
  if (isFull())
    fprintf(stderr, "Overflow");
  top++;
  S[top] = x;
}

int pop() {
  if (isEmpty())
    fprintf(stderr, "Anderflow");
  top--;
  return S[top + 1];
}


Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
#define MAX 200
#define max 1000000

void initialize();
int isEmpty();
int isFull();
void push(int);
int pop();

int top;
int S[max];

int main() {
  char c[max];
  int sum = 0;
  int a, b;

  initialize();

  while (cin >> c) {
    if (c[0] == '-') {
      a = pop();
      b = pop();
      push(b - a);
    } else if (c[0] == '+') {
      push(pop() + pop());
    } else if (c[0] == '*') {
      push(pop() * pop());
    } else {
      push(atoi(c));
    }
  }

  printf("%d\n", S[1]);

  return 0;
}

void initialize() { top = 0; }

int isEmpty() {
  if (top == 0)
    return 1;
  return 0;
}

int isFull() {
  if (top >= MAX - 1)
    return 1;
  return 0;
}

void push(int x) {
  if (isFull())
    fprintf(stderr, "Overflow");
  top++;
  S[top] = x;
}

int pop() {
  if (isEmpty())
    fprintf(stderr, "Anderflow");
  top--;
  return S[top + 1];
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int top, S[1000];

void push(int x);
int pop();

int main() {
  top = 0;
  int a, b;
  char c;

  while (scanf("%s", &c)) {
    switch (c) {
    case '+':
      b = pop();
      a = pop();
      push(a + b);
      break;
    case '-':
      b = pop();
      a = pop();
      push(a - b);
      break;
    case '*':
      b = pop();
      a = pop();
      push(a * b);
      break;
    default:
      push(atoi(&c));
      break;
    }
  }

  cout << pop() << endl;
}

void push(int x) {
  ++top;
  S[top] = x;
}

int pop() {
  --top;
  return S[top + 1];
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int top, S[1000];

void push(int x);
int pop();

int main() {
  top = 0;
  int a, b;
  char c;

  while (scanf("%s", &c) != EOF) {
    switch (c) {
    case '+':
      b = pop();
      a = pop();
      push(a + b);
      break;
    case '-':
      b = pop();
      a = pop();
      push(a - b);
      break;
    case '*':
      b = pop();
      a = pop();
      push(a * b);
      break;
    default:
      push(atoi(&c));
      break;
    }
  }

  cout << pop() << endl;
}

void push(int x) {
  ++top;
  S[top] = x;
}

int pop() {
  --top;
  return S[top + 1];
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string>
using namespace std;

const int MAX = 100;
int top, stack[MAX];

void initialize() { top = 0; }

void push(int num) { stack[++top] = num; }

int pop() {
  top--;
  return stack[top + 1];
}

int main() {
  string str;
  int a, b;

  initialize();

  while (cin >> str) {
    // cout << str << endl;
    if (str == "+") {
      a = pop();
      b = pop();
      // cout << a + b << ' ' << endl << endl;
      push(a + b);
    } else if (str == "-") {
      a = pop();
      b = pop();
      // cout << b - a << ' ' << endl << endl;
      push(b - a);
    } else if (str == "*") {
      a = pop();
      b = pop();
      // cout << a * b << ' ' << endl << endl;
      push(a * b);
    } else {
      push(stoi(str));
    }
  }

  cout << pop() << endl;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;

const int MAX = 1000;
int top, stack[MAX];

void initialize() { top = 0; }

void push(int num) { stack[++top] = num; }

int pop() {
  top--;
  return stack[top + 1];
}

int main() {
  string str;
  int a, b;

  initialize();

  while (cin >> str) {
    // cout << str << endl;
    if (str == "+") {
      a = pop();
      b = pop();
      // cout << a + b << ' ' << endl << endl;
      push(a + b);
    } else if (str == "-") {
      a = pop();
      b = pop();
      // cout << b - a << ' ' << endl << endl;
      push(b - a);
    } else if (str == "*") {
      a = pop();
      b = pop();
      // cout << a * b << ' ' << endl << endl;
      push(a * b);
    } else {
      push(stoi(str));
    }
  }

  cout << pop() << endl;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
using namespace std;

int const MAX = 100;
int top;
int s[MAX];

void initalise() { top = 0; }

bool isEmpty() { return top == 0; }

bool isFull() { return top >= MAX - 1; }

void push(int x) {
  if (isFull())
    cout << "エラー(オーバーフロー)" << endl;
  top++;
  s[top] = x;
}

int pop() {
  if (isEmpty())
    cout << "エラー(アンダーフロー)" << endl;
  top--;
  return s[top + 1];
}

int main() {
  using ite_t = istream_iterator<string>;
  ite_t it(cin);
  ite_t last;

  while (it != last) {
    if (*it == "+") {
      int ope2 = pop();
      int ope1 = pop();
      push(ope1 + ope2);
    } else if (*it == "-") {
      int ope2 = pop();
      int ope1 = pop();
      push(ope1 - ope2);
    } else if (*it == "*") {
      int ope2 = pop();
      int ope1 = pop();
      push(ope1 * ope2);
    } else {
      push(stoi(*it));
    }
    it++;
  }

  cout << pop() << endl;
}


Fixed submission:
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
using namespace std;

int const MAX = 1000;
int top;
int s[MAX];

void initalise() { top = 0; }

bool isEmpty() { return top == 0; }

bool isFull() { return top >= MAX - 1; }

void push(int x) {
  if (isFull())
    cout << "エラー(オーバーフロー)" << endl;
  top++;
  s[top] = x;
}

int pop() {
  if (isEmpty())
    cout << "エラー(アンダーフロー)" << endl;
  top--;
  return s[top + 1];
}

int main() {
  using ite_t = istream_iterator<string>;
  ite_t it(cin);
  ite_t last;

  while (it != last) {
    if (*it == "+") {
      int ope2 = pop();
      int ope1 = pop();
      push(ope1 + ope2);
    } else if (*it == "-") {
      int ope2 = pop();
      int ope1 = pop();
      push(ope1 - ope2);
    } else if (*it == "*") {
      int ope2 = pop();
      int ope1 = pop();
      push(ope1 * ope2);
    } else {
      push(stoi(*it));
    }
    it++;
  }

  cout << pop() << endl;
}
Problem p02263 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main() {
  vector<int> NumberRow;
  while (true) {
    string in;
    cin >> in;
    if (in == "END") {
      break;
    }
    if (in != "+" && in != "-" && in != "*") {
      int Num;
      stringstream ss;
      ss << in;
      ss >> Num;
      NumberRow.push_back(Num);
    }
    int M = NumberRow.size() - 2;
    if (in == "+") {
      int c = NumberRow[M] + NumberRow[M + 1];
      NumberRow[M] = c;
      NumberRow.pop_back();
    } else if (in == "-") {
      int c = NumberRow[M] - NumberRow[M + 1];
      NumberRow[M] = c;
      NumberRow.pop_back();
    } else if (in == "*") {
      int c = NumberRow[M] * NumberRow[M + 1];
      NumberRow[M] = c;
      NumberRow.pop_back();
    }
  }
  cout << NumberRow[0] << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

int main() {
  vector<int> NumberRow;
  while (true) {
    string in;
    cin >> in;
    if (cin.good() == false) {
      break;
    }
    if (in != "+" && in != "-" && in != "*") {
      int Num;
      stringstream ss;
      ss << in;
      ss >> Num;
      NumberRow.push_back(Num);
    }
    int M = NumberRow.size() - 2;
    if (in == "+") {
      int c = NumberRow[M] + NumberRow[M + 1];
      NumberRow[M] = c;
      NumberRow.pop_back();
    } else if (in == "-") {
      int c = NumberRow[M] - NumberRow[M + 1];
      NumberRow[M] = c;
      NumberRow.pop_back();
    } else if (in == "*") {
      int c = NumberRow[M] * NumberRow[M + 1];
      NumberRow[M] = c;
      NumberRow.pop_back();
    }
  }
  cout << NumberRow[0] << endl;
  return 0;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void add(int num[], int *pa) {
  *pa = (*pa) - 1;
  num[(*pa) - 1] += num[*pa];
  num[*pa] = 0;
}

void minus(int num[], int *pa) {
  *pa = (*pa) - 1;
  num[(*pa) - 1] -= num[*pa];
  num[*pa] = 0;
}

void multiply(int num[], int *pa) {
  *pa = (*pa) - 1;
  num[(*pa) - 1] *= num[*pa];
  num[*pa] = 0;
}

int main() {
  int i, n, j, k, num[200];
  char s[1000];
  int curnum = 0;
  while (scanf("%s", s) != EOF) {
    if (s[0] == '+') {
      add(num, &curnum);
    } else if (s[0] == '-') {
      minus(num, &curnum);
    } else if (s[0] == '*') {
      multiply(num, &curnum);
    } else {
      sscanf(s, "%d", num[curnum]);
      curnum++;
    }
  }
  printf("%d\n", num[0]);
}


Fixed submission:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void add(int num[], int *pa) {
  *pa = (*pa) - 1;
  num[(*pa) - 1] += num[*pa];
  num[*pa] = 0;
}

void minus(int num[], int *pa) {
  *pa = (*pa) - 1;
  num[(*pa) - 1] -= num[*pa];
  num[*pa] = 0;
}

void multiply(int num[], int *pa) {
  *pa = (*pa) - 1;
  num[(*pa) - 1] *= num[*pa];
  num[*pa] = 0;
}

int main() {
  int i, n, j, k, num[200];
  char s[1000];
  int curnum = 0;
  while (scanf("%s", s) != EOF) {
    if (s[0] == '+') {
      add(num, &curnum);
    } else if (s[0] == '-') {
      minus(num, &curnum);
    } else if (s[0] == '*') {
      multiply(num, &curnum);
    } else {
      sscanf(s, "%d", &num[curnum]);
      curnum++;
    }
  }
  printf("%d\n", num[0]);
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>
#define INF 999999
#define EPS 1.0e-6

using namespace std;

int main() {
  stack<int> s;
  char b[315];
  scanf("%[^\n]", b);

  int I = 0;
  while (I < strlen(b)) {
    if (b[I] == '+' || b[I] == '-' || b[I] == '*') {
      int j = s.top();
      s.pop();
      int i = s.top();
      s.pop();
      int k;
      if (b[I] == '+')
        k = i + j;
      if (b[I] == '-')
        k = i - j;
      if (b[I] == '*')
        k = i * j;

      s.push(k);
      I += 2;
    } else if (b[I] != ' ') {
      int j = 1;
      int num = b[I] - '0';
      I++;
      while (b[I] != ' ') {
        num *= 10;
        num += (int)(b[I] - '0');
        I++;
      }
      s.push(num);
    } else
      I++;
  }
  int ans = s.top();
  s.pop();
  printf("%d\n", ans);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <vector>
#define INF 999999
#define EPS 1.0e-6

using namespace std;

int main() {
  stack<int> s;
  char b[1050];
  scanf("%[^\n]", b);

  int I = 0;
  while (I < strlen(b)) {
    if (b[I] == '+' || b[I] == '-' || b[I] == '*') {
      int j = s.top();
      s.pop();
      int i = s.top();
      s.pop();
      int k;
      if (b[I] == '+')
        k = i + j;
      if (b[I] == '-')
        k = i - j;
      if (b[I] == '*')
        k = i * j;

      s.push(k);
      I += 2;
    } else if (b[I] != ' ') {
      int j = 1;
      int num = b[I] - '0';
      I++;
      while (b[I] != ' ') {
        num *= 10;
        num += (int)(b[I] - '0');
        I++;
      }
      s.push(num);
    } else
      I++;
  }
  int ans = s.top();
  s.pop();
  printf("%d\n", ans);

  return 0;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
#define MAX 100
int top;
int S[MAX];
void initialize() { top = 0; }
bool isEmpty() { return top == 0; }
bool isFull() { return top >= MAX - 1; }
void push(int x) {
  // if (isFull()){
  //     cout << "Error" <<endl;
  //     exit(-1);
  // }
  top++;
  S[top] = x;
}
int pop() {
  // if (isEmpty()){
  //     cout << "Error" <<endl;
  //     exit(-1);
  // }

  top--;
  return S[top + 1];
}

int main() {
  int a, b;
  initialize();
  char s[MAX];
  while (scanf("%s", s) != EOF) {
    if (s[0] == '+') {
      a = pop();
      b = pop();
      push(a + b);
    } else if (s[0] == '-') {
      b = pop();
      a = pop();
      push(a - b);
    } else if (s[0] == '*') {
      a = pop();
      b = pop();
      push(a * b);
    } else {
      push(atoi(s));
    }
  }

  printf("%d\n", pop());

  return 0;
}


Fixed submission:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// using namespace std;
#define MAX 1000
int top;
int S[MAX];
void initialize() { top = 0; }
bool isEmpty() { return top == 0; }
bool isFull() { return top >= MAX - 1; }
void push(int x) {
  // if (isFull()){
  //     cout << "Error" <<endl;
  //     exit(-1);
  // }
  top++;
  S[top] = x;
}
int pop() {
  // if (isEmpty()){
  //     cout << "Error" <<endl;
  //     exit(-1);
  // }

  top--;
  return S[top + 1];
}

int main() {
  int a, b;
  initialize();
  char s[MAX];
  while (scanf("%s", s) != EOF) {
    if (s[0] == '+') {
      a = pop();
      b = pop();
      push(a + b);
    } else if (s[0] == '-') {
      b = pop();
      a = pop();
      push(a - b);
    } else if (s[0] == '*') {
      a = pop();
      b = pop();
      push(a * b);
    } else {
      push(atoi(s));
    }
  }

  printf("%d\n", pop());

  return 0;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <ctype.h>
#include <iostream>
#include <sstream>
#include <stack>
#include <string>
using namespace std;

stack<int> s;

int main() {

  while (!s.empty()) {
    s.pop();
  }

  while (cin.eof()) {
    string tmp = "";
    cin >> tmp;
    stringstream ss;
    int a = 0;
    if (isdigit(tmp[0])) {
      ss << tmp;
      ss >> a;
      s.push(a);
    } else {
      string sign;
      sign = tmp;
      int left, right;
      right = s.top();
      s.pop();
      left = s.top();
      s.pop();
      switch (sign[0]) {
      case '+':
        s.push(left + right);
        break;
      case '-':
        s.push(left - right);
        break;
      case '*':
        s.push(left * right);
        break;
      case '/':
        s.push(left / right);
        break;
      default:
        break;
      }
      // cout << s.top() << endl;
    }
  }

  cout << s.top() << endl;

  return 0;
}

Fixed submission:
#include <ctype.h>
#include <iostream>
#include <sstream>
#include <stack>
#include <string>
using namespace std;

stack<int> s;

int main() {

  while (!s.empty()) {
    s.pop();
  }
  string tmp = "";
  while (getline(cin, tmp, ' ')) {
    stringstream ss;
    int a = 0;
    if (isdigit(tmp[0])) {
      ss << tmp;
      ss >> a;
      s.push(a);
    } else {
      string sign;
      sign = tmp;
      int left, right;
      right = s.top();
      s.pop();
      left = s.top();
      s.pop();
      switch (sign[0]) {
      case '+':
        s.push(left + right);
        break;
      case '-':
        s.push(left - right);
        break;
      case '*':
        s.push(left * right);
        break;
      case '/':
        s.push(left / right);
        break;
      default:
        break;
      }
      // cout << s.top() << endl;
    }
  }

  cout << s.top() << endl;

  return 0;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int top, S[100];

void push(int x) { S[++top] = x; }

int pop() {
  top--;
  return S[top + 1];
}

// int ToInt(char *s){
//	int res=s[0]-'0';
//	for(int i=1;s[i]!=0;i++){
//		res = res*10+s[i]-'0';
//	}
//
//	return res;
// }

int main() {
  int a, b;
  top = 0;
  char s[105];
  while (scanf("%s", s) != EOF) {
    if (s[0] == '+') {
      a = pop();
      b = pop();
      push(a + b);
    } else if (s[0] == '-') {
      b = pop();
      a = pop();
      push(a - b);
    } else if (s[0] == '*') {
      a = pop();
      b = pop();
      push(a * b);
    } else {
      push(atoi(s));
    }
  }

  printf("%d\n", pop());

  return 0;
}

Fixed submission:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int top, S[105];

void push(int x) { S[++top] = x; }

int pop() {
  top--;
  return S[top + 1];
}

// int ToInt(char *s){
//	int res=s[0]-'0';
//	for(int i=1;s[i]!=0;i++){
//		res = res*10+s[i]-'0';
//	}
//
//	return res;
// }

int main() {
  int a, b;
  top = 0;
  char s[105];
  while (scanf("%s", s) != EOF) {
    if (s[0] == '+') {
      a = pop();
      b = pop();
      push(a + b);
    } else if (s[0] == '-') {
      b = pop();
      a = pop();
      push(a - b);
    } else if (s[0] == '*') {
      a = pop();
      b = pop();
      push(a * b);
    } else {
      push(atoi(s));
    }
  }

  printf("%d\n", pop());

  return 0;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <cassert>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int top, S[1000];

void push(int x) { S[++top] = x; }

int pop() {
  top--;
  return S[top + 1];
}

int main() {
  int a, b, x, no, np;
  top = 0;
  char s[100];
  no = np = 0;
  while (scanf("%s", s) != EOF) {
    if (s[0] == '+') {
      a = pop();
      b = pop();
      push(a + b);
      no++;
    } else if (s[0] == '-') {
      b = pop();
      a = pop();
      push(a - b);
      no++;
    } else if (s[0] == '*') {
      a = pop();
      b = pop();
      push(a * b);
      no++;
    } else {
      x = atoi(s);
      assert(0 <= x && x <= 1000000);
      push(x);
      np++;
    }
  }
  assert(2 <= np && np <= 100);
  assert(1 <= no && no <= 9);

  printf("%d\n", pop());

  return 0;
}

Fixed submission:
#include <cassert>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int top, S[1000];

void push(int x) { S[++top] = x; }

int pop() {
  top--;
  return S[top + 1];
}

int main() {
  int a, b, x, no, np;
  top = 0;
  char s[100];
  no = np = 0;
  while (scanf("%s", s) != EOF) {
    if (s[0] == '+') {
      a = pop();
      b = pop();
      push(a + b);
      no++;
    } else if (s[0] == '-') {
      b = pop();
      a = pop();
      push(a - b);
      no++;
    } else if (s[0] == '*') {
      a = pop();
      b = pop();
      push(a * b);
      no++;
    } else {
      x = atoi(s);
      assert(0 <= x && x <= 1000000);
      push(x);
      np++;
    }
  }
  assert(2 <= np && np <= 100);
  assert(1 <= no && no <= 99);

  printf("%d\n", pop());

  return 0;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <stdlib.h>

#define MAX 1000

void initialize();
int isEmpty();
int isFull();
void push(int x);
int pop();

int top;
int S[100];

int main() {
  int i, n, ans, n1, n2, n3;
  char c[100];

  initialize();

  while (1) {
    if (scanf("%s", c) == EOF)
      break;

    if (c[0] == '+' || c[0] == '-' || c[0] == '*') {
      n1 = pop();
      n2 = pop();
      if (c[0] == '+')
        n3 = n1 + n2;
      if (c[0] == '-')
        n3 = n2 - n1;
      if (c[0] == '*')
        n3 = n1 * n2;
      push(n3);
    } else {
      n = atoi(c);
      push(n);
    }
  }

  printf("%d\n", pop());
  return 0;
}

void initialize() { top = 0; }

int isEmpty() { return top == 0; }

int isFull() { return top >= MAX - 1; }

void push(int x) {
  if (isFull())
    fprintf(stderr, "overflow\n");
  top++;
  S[top] = x;
}

int pop() {
  if (isEmpty())
    fprintf(stderr, "underflow\n");
  top--;
  return S[top + 1];
}

Fixed submission:
#include <stdio.h>
#include <stdlib.h>

#define MAX 1000

void initialize();
int isEmpty();
int isFull();
void push(int x);
int pop();

int top;
int S[1000];

int main() {
  int i, n, ans, n1, n2, n3;
  char c[100];

  initialize();

  while (1) {
    if (scanf("%s", c) == EOF)
      break;

    if (c[0] == '+' || c[0] == '-' || c[0] == '*') {
      n1 = pop();
      n2 = pop();
      if (c[0] == '+')
        n3 = n1 + n2;
      if (c[0] == '-')
        n3 = n2 - n1;
      if (c[0] == '*')
        n3 = n1 * n2;
      push(n3);
    } else {
      n = atoi(c);
      push(n);
    }
  }

  printf("%d\n", pop());
  return 0;
}

void initialize() { top = 0; }

int isEmpty() { return top == 0; }

int isFull() { return top >= MAX - 1; }

void push(int x) {
  if (isFull())
    fprintf(stderr, "overflow\n");
  top++;
  S[top] = x;
}

int pop() {
  if (isEmpty())
    fprintf(stderr, "underflow\n");
  top--;
  return S[top + 1];
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int top, S[100];

void push(int x) { S[++top] = x; }

int pop() { return S[top--]; }

int main() {
  char str[10];
  int a, b;
  top = 0;

  while (scanf("%s", str) != EOF) {
    if (str[0] == '+') {
      a = pop();
      b = pop();
      push(a + b);
    } else if (str[0] == '-') {
      a = pop();
      b = pop();
      push(b - a);
    } else if (str[0] == '*') {
      a = pop();
      b = pop();
      push(a * b);
    } else {
      push(atoi(str));
    }
  }
  cout << pop() << endl;
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;

int top, S[101];

void push(int x) { S[++top] = x; }

int pop() { return S[top--]; }

int main() {
  char str[10];
  int a, b;
  top = 0;

  while (scanf("%s", str) != EOF) {
    if (str[0] == '+') {
      a = pop();
      b = pop();
      push(a + b);
    } else if (str[0] == '-') {
      a = pop();
      b = pop();
      push(b - a);
    } else if (str[0] == '*') {
      a = pop();
      b = pop();
      push(a * b);
    } else {
      push(atoi(str));
    }
  }
  cout << pop() << endl;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;

#define N 100

class Stack {
public:
  void initialize();
  bool isEmpty();
  bool isFull();
  void push(const int);
  int pop();
  Stack(const int);
  ~Stack();

private:
  int top, STACKSIZE;
  vector<int> S;
};

void addition(Stack &s) {
  int a = s.pop(), b = s.pop();
  s.push(a + b);
}

void subtraction(Stack &s) {
  int a = s.pop(), b = s.pop();
  s.push(b - a);
}

void multiplication(Stack &s) {
  int a = s.pop(), b = s.pop();
  s.push(b * a);
}

int main() {
  Stack _s(N);
  char s[N];

  while (scanf("%s", s) != EOF) {
    if (s[0] == '+') {
      addition(_s);
    }

    else if (s[0] == '-') {
      subtraction(_s);
    }

    else if (s[0] == '*') {
      multiplication(_s);
    }

    else {
      _s.push(atoi(s));
    }
  }

  cout << _s.pop() << endl;

  return 0;
}

Stack::Stack(const int s) : top(0) {
  STACKSIZE = s;
  vector<int> tmp(STACKSIZE, 0);
  S = tmp;
}

Stack::~Stack() {
  // cout << "STACK DELETED" << endl;
}

void Stack::initialize() { top = 0; }

bool Stack::isEmpty() { return (top == 0); }

bool Stack::isFull() { return (top >= STACKSIZE - 1); }

void Stack::push(const int x) {
  if (isFull()) {
    cout << "Overflow!" << endl;
    exit(1);
  }

  top += 1;
  S[top] = x;
}

int Stack::pop() {
  if (isEmpty()) {
    cout << "Underflow" << endl;
    exit(2);
  }

  top -= 1;
  return (S[top + 1]);
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

#define N 101

class Stack {
public:
  void initialize();
  bool isEmpty();
  bool isFull();
  void push(const int);
  int pop();
  Stack(const int);
  ~Stack();

private:
  int top, STACKSIZE;
  vector<int> S;
};

void addition(Stack &s) {
  int a = s.pop(), b = s.pop();
  s.push(a + b);
}

void subtraction(Stack &s) {
  int a = s.pop(), b = s.pop();
  s.push(b - a);
}

void multiplication(Stack &s) {
  int a = s.pop(), b = s.pop();
  s.push(b * a);
}

int main() {
  Stack _s(N);
  char s[N];

  while (scanf("%s", s) != EOF) {
    if (s[0] == '+') {
      addition(_s);
    }

    else if (s[0] == '-') {
      subtraction(_s);
    }

    else if (s[0] == '*') {
      multiplication(_s);
    }

    else {
      _s.push(atoi(s));
    }
  }

  cout << _s.pop() << endl;

  return 0;
}

Stack::Stack(const int s) : top(0) {
  STACKSIZE = s;
  vector<int> tmp(STACKSIZE, 0);
  S = tmp;
}

Stack::~Stack() {
  // cout << "STACK DELETED" << endl;
}

void Stack::initialize() { top = 0; }

bool Stack::isEmpty() { return (top == 0); }

bool Stack::isFull() { return (top >= STACKSIZE - 1); }

void Stack::push(const int x) {
  if (isFull()) {
    cout << "Overflow!" << endl;
    exit(1);
  }

  top += 1;
  S[top] = x;
}

int Stack::pop() {
  if (isEmpty()) {
    cout << "Underflow" << endl;
    exit(2);
  }

  top -= 1;
  return (S[top + 1]);
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <stdio.h>
#include <string>
using namespace std;

#define abs(N) ((N > 0) ? N : -1 * N)
#define lt(A, B) ((A > B) ? B : A)
#define gt(A, B) ((A < B) ? B : A)
#define print(S) (cout << S)
#define println(S) (cout << S << endl)

template <typename T> class stack {
public:
  const static int MAX = 3;
  stack<T>() { pos = 0; }

  void empty() { pos = 0; }

  bool isEmpty() { return (pos == 0); }

  bool isFull() { return (pos == MAX); }

  T pop() {
    if (isEmpty()) {
      throw exception();
    }
    return body[--pos];
  }

  void push(T el) {
    if (isFull()) {
      throw exception();
    }
    body[pos++] = el;
  }

  T top() {
    if (isEmpty()) {
      throw exception();
    }
    return body[pos - 1];
  }

private:
  T body[MAX];
  int pos;
};

int main() {
  string buf, str;
  char cstr[1000];
  cin.getline(cstr, sizeof(cstr));
  str = string(cstr);
  stringstream ss(str);
  stack<int> st;
  while (getline(ss, buf, ' ')) {
    //		println("-----"+buf+"-----");
    if (buf == "+" || buf == "-" || buf == "*") {
      int r1, r2, ret;
      r1 = st.pop();
      r2 = st.pop();
      if (buf == "+") {
        ret = r1 + r2;
      }
      if (buf == "-") {
        ret = r2 - r1;
      }
      if (buf == "*") {
        ret = r1 * r2;
      }
      st.push(ret);
    } else {
      st.push(atoi(buf.c_str()));
    }
  }
  println(st.top());

  return 0;
}


Fixed submission:
#include <cstdlib>
#include <iostream>
#include <sstream>
#include <stdexcept>
#include <stdio.h>
#include <string>
using namespace std;

#define abs(N) ((N > 0) ? N : -1 * N)
#define lt(A, B) ((A > B) ? B : A)
#define gt(A, B) ((A < B) ? B : A)
#define print(S) (cout << S)
#define println(S) (cout << S << endl)

template <typename T> class stack {
public:
  const static int MAX = 10000;
  stack<T>() { pos = 0; }

  void empty() { pos = 0; }

  bool isEmpty() { return (pos == 0); }

  bool isFull() { return (pos == MAX); }

  T pop() {
    if (isEmpty()) {
      throw exception();
    }
    return body[--pos];
  }

  void push(T el) {
    if (isFull()) {
      throw exception();
    }
    body[pos++] = el;
  }

  T top() {
    if (isEmpty()) {
      throw exception();
    }
    return body[pos - 1];
  }

private:
  T body[MAX];
  int pos;
};

int main() {
  string buf, str;
  char cstr[1000];
  cin.getline(cstr, sizeof(cstr));
  str = string(cstr);
  stringstream ss(str);
  stack<int> st;
  while (getline(ss, buf, ' ')) {
    //		println("-----"+buf+"-----");
    if (buf == "+" || buf == "-" || buf == "*") {
      int r1, r2, ret;
      r1 = st.pop();
      r2 = st.pop();
      if (buf == "+") {
        ret = r1 + r2;
      }
      if (buf == "-") {
        ret = r2 - r1;
      }
      if (buf == "*") {
        ret = r1 * r2;
      }
      st.push(ret);
    } else {
      st.push(atoi(buf.c_str()));
    }
  }
  println(st.top());

  return 0;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main() {
  stack<int> num;
  string s;
  int a, b;
  while (cin >> s) {
    if (s >= "1")
      num.push(atoi(s.c_str()));
    else if (s == "+" || s == "-" || s == "*") {
      a = num.top();
      num.pop();
      b = num.top();
      num.pop();
      if (s == "+")
        num.push(a + b);
      else if (s == "-")
        num.push(b - a);
      else
        num.push(a * b);
    }
  }
  cout << num.top() << endl;
}

Fixed submission:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <stack>
#include <string>

using namespace std;

int main() {
  stack<int> num;
  string s;
  int a, b;
  while (cin >> s) {
    if (s >= "0")
      num.push(atoi(s.c_str()));
    else if (s == "+" || s == "-" || s == "*") {
      a = num.top();
      num.pop();
      b = num.top();
      num.pop();
      if (s == "+")
        num.push(a + b);
      else if (s == "-")
        num.push(b - a);
      else
        num.push(a * b);
    }
  }
  cout << num.top() << endl;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main() {
  int a, b;
  stack<int> st;
  string op;
  while (cin >> op) {
    cin >> op;
    if (isdigit(op[0]))
      st.push(stoi(op));
    else {
      b = st.top();
      st.pop();
      a = st.top();
      st.pop();
      switch (op[0]) {
      case '+':
        st.push(a + b);
        break;
      case '-':
        st.push(a - b);
        break;
      case '*':
        st.push(a * b);
        break;
      }
    }
  }
  cout << st.top() << endl;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main() {
  int a, b;
  stack<int> st;
  string op;
  while (cin >> op) {
    // cout << op << endl;
    if (isdigit(op[0]))
      st.push(stoi(op));
    else {
      b = st.top();
      st.pop();
      a = st.top();
      st.pop();
      switch (op[0]) {
      case '+':
        st.push(a + b);
        break;
      case '-':
        st.push(a - b);
        break;
      case '*':
        st.push(a * b);
        break;
      }
    }
  }
  cout << st.top() << endl;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):

#include <iostream>
#include <stack>
#include <stdlib.h>
#include <string>
using namespace std;
int main() {
  string word;
  stack<int> S;
  int n, n1, n2, p;
  while (cin >> word) {
    if (word == "+") {
      n1 = S.top();
      S.pop();
      n2 = S.top();
      S.pop();
      n = n1 + n2;
      S.push(n);
    } else if (word == "-") {
      n1 = S.top();
      S.pop();
      n2 = S.top();
      S.pop();
      n = n2 - n1;
      S.push(n);
    } else if (word == "*") {
      n1 = S.top();
      S.pop();
      n2 = S.top();
      S.pop();
      n = n1 * n2;
      S.push(n);
    } else {
      p = atoi(word.c_str());
      S.push(p);
    }
  }
  cout << S.top() << endl;
  return S.top();
}

Fixed submission:

#include <iostream>
#include <stack>
#include <stdlib.h>
#include <string>
using namespace std;
int main() {
  string word;
  stack<int> S;
  int n, n1, n2, p;
  while (cin >> word) {
    if (word == "+") {
      n1 = S.top();
      S.pop();
      n2 = S.top();
      S.pop();
      n = n1 + n2;
      S.push(n);
    } else if (word == "-") {
      n1 = S.top();
      S.pop();
      n2 = S.top();
      S.pop();
      n = n2 - n1;
      S.push(n);
    } else if (word == "*") {
      n1 = S.top();
      S.pop();
      n2 = S.top();
      S.pop();
      n = n1 * n2;
      S.push(n);
    } else {
      p = atoi(word.c_str());
      S.push(p);
    }
  }
  cout << S.top() << endl;
  return 0;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

class Stack {
private:
  int sp;
  int S[10];

public:
  Stack() { sp = 0; }
  bool isFull() const { return sp == 100; }
  bool isEmpty() const { return sp == 0; }
  void push(int d) {
    if (isFull())
      return;
    S[sp] = d;
    sp++;
  }
  int top() {
    if (isEmpty())
      return -1;
    return S[sp - 1];
  }
  void pop() {
    if (isEmpty())
      return;
    sp--;
  }
};

int main() {
  Stack s;
  string expr;
  getline(cin, expr);
  istringstream is(expr);

  while (!is.eof()) {
    string op;
    is >> op;
    int tmp1, tmp2;

    switch (op[0]) {
    case '+':
      tmp1 = s.top();
      s.pop();
      tmp2 = s.top();
      s.pop();
      s.push(tmp1 + tmp2);
      break;
    case '-':
      tmp1 = s.top();
      s.pop();
      tmp2 = s.top();
      s.pop();
      s.push(tmp2 - tmp1);
      break;
    case '*':
      tmp1 = s.top();
      s.pop();
      tmp2 = s.top();
      s.pop();
      s.push(tmp1 * tmp2);
      break;
    default:
      istringstream is2(op);
      is2 >> tmp1;
      s.push(tmp1);
      break;
    }
  }

  cout << s.top() << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;

class Stack {
private:
  int sp;
  int S[100];

public:
  Stack() { sp = 0; }
  bool isFull() const { return sp == 100; }
  bool isEmpty() const { return sp == 0; }
  void push(int d) {
    if (isFull())
      return;
    S[sp] = d;
    sp++;
  }
  int top() {
    if (isEmpty())
      return -1;
    return S[sp - 1];
  }
  void pop() {
    if (isEmpty())
      return;
    sp--;
  }
};

int main() {
  Stack s;
  string expr;
  getline(cin, expr);
  istringstream is(expr);

  while (!is.eof()) {
    string op;
    is >> op;
    int tmp1, tmp2;

    switch (op[0]) {
    case '+':
      tmp1 = s.top();
      s.pop();
      tmp2 = s.top();
      s.pop();
      s.push(tmp1 + tmp2);
      break;
    case '-':
      tmp1 = s.top();
      s.pop();
      tmp2 = s.top();
      s.pop();
      s.push(tmp2 - tmp1);
      break;
    case '*':
      tmp1 = s.top();
      s.pop();
      tmp2 = s.top();
      s.pop();
      s.push(tmp1 * tmp2);
      break;
    default:
      istringstream is2(op);
      is2 >> tmp1;
      s.push(tmp1);
      break;
    }
  }

  cout << s.top() << endl;
  return 0;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <stack>
using namespace std;

int main() {
  stack<int> t;
  string s;
  int a, b, c;
  while (cin >> s) {
    if ("0" <= s && s <= "9") {
      if (s.size() >= 2) {
        a = s.size();
        b = 1;
        for (int i = 0; i < a - 1; i++)
          b *= 10;
        c = 0;
        for (int i = 0; i < a; i++) {
          c += (s[i] - '0') * b;
          b /= 10;
        }
        t.push(c);
      } else
        t.push(s[0] - '0');
    } else if (s == "+") {
      a = t.top();
      t.pop();
      b = t.top();
      t.pop();
      t.push(a + b);
    } else if (s == "-") {
      a = t.top();
      t.pop();
      b = t.top();
      t.pop();
      t.push(b - a);
    } else if (s == "*") {
      a = t.top();
      t.pop();
      b = t.top();
      t.pop();
      t.push(a * b);
    }
  }
  cout << t.top() << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <stack>
using namespace std;

int main() {
  stack<int> t;
  string s;
  int a, b, c;
  while (cin >> s) {
    if ('0' <= s[0] && s[0] <= '9') {
      if (s.size() >= 2) {
        a = s.size();
        b = 1;
        for (int i = 0; i < a - 1; i++)
          b *= 10;
        c = 0;
        for (int i = 0; i < a; i++) {
          c += (s[i] - '0') * b;
          b /= 10;
        }
        t.push(c);
      } else
        t.push(s[0] - '0');
    } else if (s == "+") {
      a = t.top();
      t.pop();
      b = t.top();
      t.pop();
      t.push(a + b);
    } else if (s == "-") {
      a = t.top();
      t.pop();
      b = t.top();
      t.pop();
      t.push(b - a);
    } else if (s == "*") {
      a = t.top();
      t.pop();
      b = t.top();
      t.pop();
      t.push(a * b);
    }
  }
  cout << t.top() << endl;
  return 0;
}
Problem p02263 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;

void push(int A);
int pop(void);

int S[1000];
int Top = 0;

int main(void) {

  string str;
  int a, b;
  Top = 0;
  while (str != "-1") {
    cin >> str;

    if (str[0] == '+') {
      a = pop();
      b = pop();
      push(a + b);
    } else if (str[0] == '-') {
      b = pop();
      a = pop();
      push(a - b);
    } else if (str[0] == '*') {
      a = pop();
      b = pop();
      push(a * b);
    } else {
      push(atoi(str.c_str()));
    }
  }
  printf("%d\n", pop());

  return 0;
}

void push(int A) { S[++Top] = A; }

int pop(void) {
  Top--;
  return S[Top + 1];
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;

void push(int A);
int pop(void);

int S[1000];
int Top = 0;

int main(void) {

  string str;
  int a, b;
  Top = 0;
  while (cin >> str) {

    if (str[0] == '+') {
      a = pop();
      b = pop();
      push(a + b);
    } else if (str[0] == '-') {
      b = pop();
      a = pop();
      push(a - b);
    } else if (str[0] == '*') {
      a = pop();
      b = pop();
      push(a * b);
    } else {
      push(atoi(str.c_str()));
    }
  }
  printf("%d\n", pop());

  return 0;
}

void push(int A) { S[++Top] = A; }

int pop(void) {
  Top--;
  return S[Top + 1];
}