alexjercan/bugnet huggingface dataset:
Problem p02414 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>

int a[100][100];
int b[100][100];
double c[100][100];

int main() {
  int n, m, l;
  scanf("%d %d %d", &n, &m, &l);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &a[i][j]);
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < l; j++) {
      scanf("%d", &b[i][j]);
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      c[i][j] = 0;
    }
  }

  for (int bi = 0; bi < n; bi += 16) {
    for (int bj = 0; bj < l; bj += 16) {
      for (int bk = 0; bk < m; bk += 16) {
        for (int i = bi; i < bi + 16 && i < n; i++) {
          for (int j = bj; j < bj + 16 && j < l; j++) {
            for (int k = bk; k < bk + 16 && k < m; k++) {
              c[i][j] += a[i][k] * b[k][j];
            }
          }
        }
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      if (j != 0) {
        printf(" ");
      }
      printf("%.0lf", c[i][j]);
    }
    printf("\\n");
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>

using namespace std;

int a[100][100];
int b[100][100];
int c[100][100];

int main() {
  int n, m, l;
  cin >> n >> m >> l;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < l; j++) {
      cin >> b[i][j];
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      c[i][j] = 0;
    }
  }
  for (int bi = 0; bi < n; bi += 16) {
    for (int bj = 0; bj < l; bj += 16) {
      for (int bk = 0; bk < m; bk += 16) {
        for (int i = bi; i < bi + 16; i++) {
          for (int j = bj; j < bj + 16; j++) {
            for (int k = bk; k < bk + 16; k++) {
              c[i][j] += a[i][k] * b[k][j];
            }
          }
        }
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      if (j != 0) {
        cout << " ";
      }
      cout << c[i][j];
    }
    cout << endl;
  }
  return 0;
}
Problem p02414 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int n, m, l;
  cin >> n >> m >> l;
  int A[100][100] = {};
  int B[100][100] = {};
  long int C[100][100] = {};

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> A[i][j];
    }
  }

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < l; j++) {
      cin >> B[i][j];
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      for (int k = 0; k < m; k++) {
        C[i][j] += A[i][k] * B[k][j];
      }
      cout << C[i][j];
      if (j == l - 1)
        cout << endl;
      else
        cout << " ";
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, m, l;
  cin >> n >> m >> l;
  int A[100][100] = {};
  int B[100][100] = {};
  int C[100][100] = {};

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; m++) {
      cin >> A[i][j];
    }
  }

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < l; j++) {
      cin >> B[i][j];
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      for (int k = 0; k < m; k++) {
        C[i][j] += A[i][m] * B[m][j];
      }
      cout << C[i][j];
      if (j == l - 1)
        cout << endl;
      else
        cout << " ";
    }
  }

  return 0;
}
Problem p02414 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
using namespace std;

int main() {
  int n, m, l;
  scanf("%d%d%d", &n, &m, &l);
  long long int a[100][100], b[100][100];
  long long int c[100][100] = {};
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &a[i][j]);
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < l; j++) {
      scanf("%d", &b[i][j]);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      for (int k = 0; k < m; k++) {
        c[i][j] += a[i][k] * b[k][j];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      if (j < l - 1) {
        printf("%lld ", c[i][j]);
      } else {
        printf("%lld", c[i][j]);
      }
    }
    printf("\\n");
  }
}


Error: -11

Fixed submission:

#include <stdio.h>
using namespace std;

int main() {
  int n, m, l;
  scanf("%d%d%d", &n, &m, &l);
  int a[n][m], b[m][l], c[n][l];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", a[i][j]);
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < l; j++) {
      scanf("%d", b[m][l]);
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      for (int k = 0; k < m; k++) {
        c[i][j] += a[i][k] * b[k][j];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      if (j < l - 2) {
        printf("%d ", c[i][j]);
      } else {
        printf("%d", c[i][j]);
      }
    }
    printf("\\n");
  }
}
Problem p02414 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m, l;
  cin >> n >> m >> l;
  int a[200][200];
  int b[200][200];
  long long c[200][200] = {};

  for (int i = 1; i <= n; i++) {
    for (int g = 1; g <= m; g++) {
      cin >> a[i][g];
    }
  }
  for (int i = 1; i <= m; i++) {
    for (int g = 1; g <= l; g++) {
      cin >> b[i][g];
    }
  }

  for (int h = 1; h <= l; h++) {
    for (int i = 1; i <= n; i++) {
      for (int g = 1; g <= m; g++) {
        c[i][h] += a[i][g] * b[g][h];
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int g = 1; g <= l; g++) {
      cout << c[i][g];
      if (g < l) {
        cout << " ";
      }
    }
    cout << endl;
  }

  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  int n, m, l;
  cin >> n >> m >> l;
  int a[100][100];
  int b[100][100];
  int c[100][100] = {};

  for (int i = 1; i <= n; i++) {
    for (int g = 1; g <= m; g++) {
      cin >> a[i][g];
    }
  }
  for (int i = 1; i <= m; i++) {
    for (int g = 1; g <= l; g++) {
      cin >> b[i][g];
    }
  }

  for (int h = 1; h <= l; h++) {
    for (int i = 1; i <= n; i++) {
      for (int g = 1; g <= m; g++) {
        c[i][h] += a[i][g] * b[g][h];
      }
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int g = 1; g <= l; g++) {
      cout << c[i][g];
      if (g < l) {
        cout << " ";
      }
    }
    cout << endl;
  }

  return 0;
}
Problem p02414 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  int n, m, l;
  int **elm1;
  int **elm2;
  long res;

  cin >> n >> m >> l;

  elm1 = new int *[m];
  for (int i = 0; i < m; ++i) {
    elm1[i] = new int[n];
  }

  elm2 = new int *[l];
  for (int i = 0; i < l; ++i) {
    elm2[i] = new int[m];
  }

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> elm1[j][i];
    }
  }

  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < l; ++j) {
      cin >> elm2[j][i];
    }
  }

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < l; ++j) {

      res = 0;
      for (int k = 0; k < m; ++k) {
        res += elm1[k][i] * elm2[j][k];
      }
      cout << (j ? " " : "") << res;
    }
    cout << endl;
  }
}

Error: 0

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int n, m, l;
  int **elm1;
  int **elm2;
  int res;

  cin >> n >> m >> l;

  elm1 = new int *[m];
  for (int i = 0; i < m; ++i) {
    elm1[i] = new int[n];
  }

  elm2 = new int *[n];
  for (int i = 0; i < n; ++i) {
    elm2[i] = new int[l];
  }

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> elm1[j][i];
    }
  }

  for (int i = 0; i < l; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> elm2[j][i];
    }
  }

  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < l; ++j) {

      res = 0;
      for (int k = 0; k < m; ++k) {
        res += elm1[k][i] * elm2[j][k];
      }
      cout << (j ? " " : "") << res;
    }
    cout << endl;
  }
}
Problem p02414 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main() {
  long long n, m, l, i, j, k, sum = 0, A[100][100] = {}, B[100][100] = {},
                              C[100][100] = {};
  cin >> n >> m >> l;
  for (i = 0; i < n; i++) {
    for (k = 0; k < m; k++) {
      cin >> A[i][k];
    }
  }
  for (k = 0; k < m; k++) {
    for (j = 0; j < l; j++) {
      cin >> B[k][j];
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < l; j++) {
      for (k = 0; k < m; k++) {
        sum += A[i][k] * B[k][j];
      }
      C[i][j] = sum;
      sum = 0;
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < l; j++) {

      if (j != l - 1) {
        cout << C[i][j] << " ";
      } else {
        cout << C[i][j];
      }
    }
    cout << endl;
  }
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int A[10000][10000] = {}, B[10000][10000] = {}, C[10000][10000] = {}, n, m, l,
      i, j, k, sum = 0;
  cin >> n >> m >> l;
  for (i = 0; i < n; i++) {
    for (k = 0; k < m; k++) {
      cin >> A[i][k];
    }
  }
  for (k = 0; k < m; k++) {
    for (j = 0; j < l; j++) {
      cin >> B[k][j];
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < l; j++) {
      for (k = 0; k < m; k++) {
        sum += A[i][k] * B[k][j];
      }
      C[i][j] = sum;
      sum = 0;
    }
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < l; j++) {
      cout << C[i][j] << " ";
    }
    cout << endl;
  }
  return 0;
}
Problem p02414 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

int main() {
  long long A[100][100], B[100][100], C[100][100];
  int n, m, l;

  cin >> n >> m >> l;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < m; j++)
      cin >> A[i][j];
  for (int i = 0; i < m; i++)
    for (int j = 0; j < l; j++)
      cin >> B[i][j];

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      C[i][j] = 0;
      for (int k = 0; k < m; k++) {
        C[i][j] += A[i][k] * B[k][j];
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      if (j)
        cout << " ";
      cout << C[i][j];
    }
    cout << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  double A[1000][1000], B[1000][1000], C[1000][1000];
  int n, m, l;

  cin >> n >> m >> l;

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      cin >> A[i][j];
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= l; j++)
      cin >> B[i][j];

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= l; j++)
      for (int k = 1; k <= m; k++)
        C[i][j] += A[i][k] * B[k][j];

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= l; j++) {
      if (j != 1)
        cout << " ";
      cout << C[i][j];
    }
    cout << endl;
  }

  return 0;
}
Problem p02414 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <cstdio>

int main() {
  int n, m, l;
  scanf("%d%d%d", &n, &m, &l);
  int a[n][m], b[m][l];
  long c[n][l];

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      c[i][j] = 0;
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &a[i][j]);
    }
  }

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < l; j++) {
      scanf("%d", &b[i][j]);
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      c[i][j] = 0;
      for (int k = 0; k < m; k++) {
        c[i][j] = c[i][j] + a[i][k] * b[k][j];
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      printf("%ld", c[i][j]);
      if (j == l - 1) {
        printf("\\n");
      } else {
        printf(" ");
      }
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

// n×mの行列Aとm×lの列ベクトルbを読み込み、Aとbの積 c(n×l)を出力するプログラム

#include <cstdio>

int main() {
  int n, m, l;
  scanf("%d%d%d", &n, &m, &l);
  int a[n][m], b[m][l], c[n][l];

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      c[i][j] = 0;
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      scanf("%d", &a[i][j]);
    }
  }

  printf("sec 1");

  for (int i = 0; i < m; i++) {
    for (int j = 0; j < l; l++) {
      scanf("%d", &b[i][j]);
    }
  }

  printf("sec 2");

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      c[i][j] = c[i][j] + a[i][j] * b[j][i];
    }
  }

  printf("sec 3");

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      printf("%d\\n", c[i][j]);
      if (j == l - 1) {
        printf("\\n");
      } else {
        printf(" ");
      }
    }
  }

  return 0;
}
Problem p02414 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;
int main(void) {
  // Your code here!
  int n, m, l;
  cin >> n >> m >> l;
  long long ue[101][101] = {};
  long long sit[101][101] = {};
  long long kam[101][101] = {};
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> ue[i][j];
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < l; j++) {
      cin >> sit[i][j];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      for (int k = 0; k < m; k++) {
        kam[i][j] += ue[i][k] * sit[k][j];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      cout << kam[i][j];
      if (j != l - 1)
        cout << " ";
    }
    cout << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <iostream>
using namespace std;
int main(void) {
  // Your code here!
  int n, m, l;
  cin >> n >> m >> l;
  int ue[100][100] = {};
  int sit[100][100] = {};
  int kam[10000][10000] = {};
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < m; j++) {
      cin >> ue[i][j];
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < l; j++) {
      cin >> sit[i][j];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      for (int k = 0; k < m; k++) {
        kam[i][j] += ue[i][k] * sit[k][j];
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < l; j++) {
      cout << kam[i][j];
      if (j != l - 1)
        cout << " ";
    }
    cout << endl;
  }
  return 0;
}
Problem p02414 (C++) — buggy submission, verdict: Runtime Error

//============================================================================
// Name        : ITP_1_7_D.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
using namespace std;

int main() {
  vector<vector<long long>> a, b;
  long long temp;
  int l, m, n;
  cin >> n >> m >> l;
  a.resize(n);
  b.resize(m);
  for (int i = 0; i < n; ++i) {
    a[i].reserve(m);
    for (int j = 0; j < m; ++j) {
      cin >> temp;
      a[i].push_back(temp);
    }
  }
  for (int i = 0; i < m; ++i) {
    b[i].reserve(l);
    for (int j = 0; j < l; ++j) {
      cin >> temp;
      b[i].push_back(temp);
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < l; ++j) {
      temp = 0;
      for (int k = 0; k < m; ++k) {
        temp += a[i][k] * b[k][j];
      }
      cout << temp;
      if (j != l - 1) {
        cout << " ";
      } else {
        cout << endl;
      }
    }
  }
  return 0;
}

Error: -11

Fixed submission:

//============================================================================
// Name        : ITP_1_7_D.cpp
// Author      :
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int l, m, n, tmp;
  vector<vector<int>> a, b;
  cin >> n >> m >> l;
  a.resize(n);
  for (int i = 0; i < n; ++i) {
    a[i].resize(m);
  }
  b.resize(m);
  for (int i = 0; i < n; ++i) {
    b[i].resize(l);
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < m; ++j) {
      cin >> a[i][j];
    }
  }
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < l; ++j) {
      cin >> b[i][j];
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < l; ++j) {
      tmp = 0;
      for (int k = 0; k < m; ++k) {
        tmp += a[i][k] * b[k][j];
      }
      cout << tmp;
      if (j != l - 1)
        cout << " ";
    }
    cout << endl;
  }
  return 0;
}
Problem p02416 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  while (1) {
    string str;
    cin >> str;
    if (str == "0") {
      break;
    }
    int sum = 0;
    for (int i = 0; i < (int)str.size(); i++) {
      sum += str[i] - '0';
    }
    cout << sum << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int main() {
  int x;

  while (true) {
    cin >> x;
    int result = 0;
    if (x == 0) {
      break;
    } else {
      result += x / 1000;
      x = x % 1000;
      result += x / 100;
      x = x % 100;
      result += x / 10;
      x = x % 10;
      result += x;
    }
    cout << result << endl;
  }
  return 0;
}
Problem p02416 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <string>
int main() {
  using namespace std;
  string s;
  int ans, x, y, size, i, j;
  ans = 0;
  i = 0;
  x = 1;
  while (x) {
    cin >> s;
    if (s == "0") {
      break;
    }
    size = s.size();
    for (j = 0; j < size; j++) {
      y = s[j] - '0';
      ans = ans + y;
    }
    cout << ans << endl;
    ans = 0;
  }
  return 0;
}


Error: 0

Fixed submission:

#include <iostream>
#include <string>
int main() {
  using namespace std;
  string s;
  int ans, x, y, size, i, j;
  ans = 0;
  i = 0;
  x = 1;
  while (x) {
    cin >> s;
    if (s == "0") {
      break;
    }
    x = stoi(s);
    size = s.size();
    for (j = 0; j < size; j++) {
      y = s[j] - '0';
      ans = ans + y;
    }
    cout << ans << endl;
    ans = 0;
  }
  return 0;
}
Problem p02416 (C++) — buggy submission, verdict: Runtime Error

#include <cstdio>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  string str;
  int count = 0;
  int sum;
  int a;
  int offset;
  string splitString;
  vector<string> strList;

  while (1) {
    cin >> str;

    if (str == "0") {
      break;
    }

    strList.push_back(str);
    count++;
  }

  for (int i = 0; i < count; i++) {
    offset = 0;
    sum = 0;
    while (offset < strList[i].length()) {
      splitString = strList[i].substr(offset, 8);
      a = stoi(splitString);
      while (a > 0) {
        sum += a % 10;
        a /= 10;
      }
      offset += 8;
    }
    cout << sum << "\\n";
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cstdio>
#include <iostream>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int main() {
  ios::sync_with_stdio(false);
  string str;
  int count = 0;
  int sum;
  int a;
  int offset;
  string splitString;
  vector<string> strList;

  while (1) {
    cin >> str;

    if (str == "0") {
      break;
    }

    strList.push_back(str);
    count++;
  }

  for (int i = 0; i < count; i++) {
    offset = 0;
    sum = 0;
    while (offset <= strList[i].length()) {
      splitString = strList[i].substr(offset, 8);
      a = stoi(splitString);
      while (a > 0) {
        sum += a % 10;
        a /= 10;
      }
      offset += 8;
    }
    cout << sum << "\\n";
  }

  return 0;
}
Problem p02416 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cctype>
#include <math.h>
#include <stdio.h>

int main() {
  char x[1001] = {0};
  int sum = 0;

  while (1) {
    sum = 0;
    scanf("%s", &x);
    for (int i = 0; x[i] != 0; i++) {
      sum += x[i] - '0';
    }

    if (sum == 0) {
      break;
    } else {
      printf("%d\\n", sum);
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <math.h>
#include <stdio.h>

int main() {
  char x[1001] = {0};
  int sum = 0;

  while (1) {
    sum = 0;
    scanf("%s", &x);
    for (int i = 1; i <= 1000; i++) {
      sum += x[i] - '0';
    }

    if (sum == 0) {
      break;
    } else {
      printf("%d\\n", sum);
    }
  }

  return 0;
}
Problem p02416 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <string>

using namespace std;

int ctoi(char c) {
  if (c >= '0' && c <= '9')
    return c - '0';
  return 0;
}

int main() {
  string x;
  while (true) {
    cin >> x;
    if (x == "0")
      return 0;

    long sum = 0;
    int p = 0;
    if (x[0] == '-')
      p = 1;
    for (int i = p; i < x.size(); i++) {
      int x_int = ctoi(x[i]);
      // cout << "x_int: " << x_int << endl;
      sum += x_int;
    }
    cout << sum << endl;
  }
}


Error: TLE

Fixed submission:

#include <iostream>

using namespace std;

int main() {
  int x = 1;
  while (true) {
    cin >> x;
    if (x == 0)
      return 0;

    int a, b, c, d;
    a = x / 1000;
    b = (x - a * 1000) / 100;
    c = (x - a * 1000 - b * 100) / 10;
    d = (x - a * 1000 - b * 100 - c * 10);
    int sum = a + b + c + d;
    cout << sum << endl;
  }
}
Problem p02416 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int main() {
  int sum[1001] = {};
  int count = 0;

  while (1) {
    char in[1001];
    int i = 0;
    cin.getline(in, sizeof(in));

    if (in[0] == '0') {
      break;
    }

    for (i = 0; in[i] != '\\0'; i++) {
      if (in[i] == '1') {
        sum[count] = sum[count] + 1;
      } else if (in[i] == '2') {
        sum[count] = sum[count] + 2;
      } else if (in[i] == '3') {
        sum[count] = sum[count] + 3;
      } else if (in[i] == '4') {
        sum[count] = sum[count] + 4;
      } else if (in[i] == '5') {
        sum[count] = sum[count] + 5;
      } else if (in[i] == '6') {
        sum[count] = sum[count] + 6;
      } else if (in[i] == '7') {
        sum[count] = sum[count] + 7;
      } else if (in[i] == '8') {
        sum[count] = sum[count] + 8;
      } else if (in[i] == '9') {
        sum[count] = sum[count] + 9;
      }
    }
    count++;
  }
  for (int s = 0; s < count; s++) {
    cout << sum[s] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int main() {
  int sum[1001] = {};
  int count = 0;

  while (1) {
    char in[1000];
    int i = 0;
    cin.getline(in, sizeof(in));

    if (in[0] == '0') {
      break;
    }

    for (i = 0; in[i] != '\\0'; i++) {
      if (in[i] == '1') {
        sum[count] = sum[count] + 1;
      } else if (in[i] == '2') {
        sum[count] = sum[count] + 2;
      } else if (in[i] == '3') {
        sum[count] = sum[count] + 3;
      } else if (in[i] == '4') {
        sum[count] = sum[count] + 4;
      } else if (in[i] == '5') {
        sum[count] = sum[count] + 5;
      } else if (in[i] == '6') {
        sum[count] = sum[count] + 6;
      } else if (in[i] == '7') {
        sum[count] = sum[count] + 7;
      } else if (in[i] == '8') {
        sum[count] = sum[count] + 8;
      } else if (in[i] == '9') {
        sum[count] = sum[count] + 9;
      }
    }
    count++;
  }
  for (int s = 0; s < count; s++) {
    cout << sum[s] << endl;
  }
  return 0;
}
Problem p02416 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

int main(void) {
  string str;
  int sum;
  int i;

  while (1) {
    cin >> str;
    sum = 0;
    for (i = 0; str[i] != '\\0'; i++) {
      sum += (str[i] - '0');
    }

    if (sum == 0) {
      break;
    } else {
      cout << sum << endl;
    }
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;

int main(void) {
  char c;
  int sum;

  while (1) {
    sum = 0;
    while (1) {
      cin >> c;
      if (c == '\\n') {
        break;
      } else {
        sum += (c - '0');
      }
    }

    cout << sum << endl;
    if (sum == 0) {
      break;
    } else {
      cout << sum << endl;
    }
  }

  return 0;
}