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

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

std::vector<size_t>
SetPosList(std::string str,
           std::string keystr) // 先頭に目的のキーがある場合を除く
{
  std::vector<size_t> posList;

  size_t i = 0;

  while (1) {
    i = str.find(keystr, i);

    if (i == std::string::npos) {
      break;
    }

    posList.push_back(i);

    ++i;
  }

  return posList;
}

int main() {
  std::string str;

  getline(std::cin, str);

  std::vector<size_t> posP, posA;

  size_t i = 0;

  if (str.find("Apple", i) == 0) {
    str.replace(0, 5, "Peach");
  } else if (str.find("Peach", i) == 0) {
    str.replace(0, 5, "Apple");
  }

  posA = SetPosList(str, "apple");

  posP = SetPosList(str, "peach");

  for (size_t j = 0; j < posP.size(); ++j) {
    str.replace(posP[j], 5, "apple");
  }

  for (size_t j = 0; j < posA.size(); ++j) {
    str.replace(posA[j], 5, "peach");
  }

  std::cout << str << '\\n';
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::erase: __pos (which is 18446744073709551615) > this->size() (which is 0)


Fixed submission:

#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>

int main() {
  const std::string p = "peach", a = "apple", tempWord = "---";

  std::string str;

  std::vector<std::string> words;

  std::stringstream ss;

  while (1) {
    if (std::cin.eof()) {
      break;
    }

    std::getline(std::cin, str);

    const size_t periodNum = str.length() - 1;

    std::string period = "";

    if (!('A' <= str[periodNum] && str[periodNum] <= 'z')) {

      period = str[periodNum];

      str.erase(periodNum);
    }

    ss << str;

    while (1) {
      std::string word;

      ss >> word;

      words.push_back(word);

      if (ss.eof()) {
        break;
      }
    }

    // for_eachで一気に書ける？
    std::replace_if(
        words.begin(), words.end(), [p](std::string w) { return w == p; },
        tempWord);

    std::replace_if(
        words.begin(), words.end(), [a](std::string w) { return w == a; }, p);

    std::replace_if(
        words.begin(), words.end(),
        [tempWord](std::string w) { return w == tempWord; }, a);

    // std::for_each(words.begin(),words.end(),[](std::string w){std::cout << w
    // << ' ';});

    words[words.size() - 1] += period;

    for (size_t i = 0; i < words.size(); ++i) {
      std::cout << words[i] << ' ';
    }

    std::cout << '\\n';

    ss.str("");

    ss.clear();

    words.clear();

    str.clear();
  }
}
Problem p00051 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int n;
  int ret[8];
  cin >> n;
  while (n-- > 0) {
    string s;
    cin >> s;
    for (int i = 0; i < 8; i++)
      ret[i] = s[i] - '0';
    int Min = 0, Max = 0;
    sort(ret, ret + 8);
    for (int i = 0; i < 8; i++) {
      Max += pow(10, i) * ret[i];
    }

    for (int i = 7; i >= 0; i--) {
      Min += pow(10, i) * ret[7 - i];
    }
    int differential = Max - Min;

    cout << differential << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int n;
  int ret[8];
  cin >> n;
  while (n-- > 0) {
    string s;
    cin >> s;
    for (int i = 0; i < 8; i++)
      ret[i] = s[i - '0'];
    int Min = 0, Max = 0;
    sort(ret, ret + 8);
    for (int i = 0; i < 8; i++) {
      Max += pow(10, i) * ret[i];
    }

    for (int i = 7; i >= 0; i--) {
      Min += pow(10, i) * ret[7 - i];
    }
    int differential = Max - Min;

    cout << differential << endl;
  }
  return 0;
}
Problem p00051 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int n, max, min, kkk;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string input = "", mmm, mai = "", mii = "";
    int a[8] = {};
    cin >> input;
    for (int j = 0; j < 8; j++) {
      mmm = input[j];
      a[j] = stoi(mmm);
    }
    sort(a, end(a));
    kkk = 0;
    for (int j = 0; j < 8; j++) {
      if (a[7] == 0) {
        mai = "0";
        mii = "0";
        break;
      }
      mai += to_string(a[7 - j]);
      if (a[j] == 0 && kkk == 0) {
      } else {
        mii += to_string(a[j]);
        kkk++;
      }
    }
    max = stoi(mai);
    min = stoi(mii);
    cout << max - min << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;

int main() {
  int n, max, min, kkk;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string input = "", mmm, mai = "", mii = "";
    int a[8] = {};
    cin >> input;
    for (int j = 0; j < 8; j++) {
      mmm = input[j];
      a[j] = stoi(mmm);
    }
    sort(a, end(a));
    kkk = 0;
    for (int j = 0; j < 8; j++) {
      mai += to_string(a[7 - j]);
      if (a[j] == 0 && kkk == 0) {
      } else {
        mii += to_string(a[j]);
        kkk++;
      }
    }
    max = stoi(mai);
    min = stoi(mii);
    cout << max - min << endl;
  }
  return 0;
}
Problem p00051 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define LL long long
#define ULL unsigned long long
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, x, n) for (int i = x; i < (n); i++)
#define SORT(n) sort((n).begin(), (n).end())
#define SORT2(n) sort((n).begin(), (n).end(), greater<int>())
using namespace std;
int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  cin >> n;
  string s;

  REP(i, n) {

    cin >> s;

    string ma = s, mi = s;

    SORT2(ma);
    SORT(mi);

    int res1 = numeric_limits<int>::min();
    int res2 = numeric_limits<int>::max();

    res1 = max(res1, stoi(ma));
    res2 = min(res2, stoi(mi));

    cout << res1 - res2 << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
#define ULL unsigned long long
#define REP(i, n) for (int i = 0; i < (n); i++)
#define REP2(i, x, n) for (int i = x; i < (n); i++)
#define SORT(n) sort((n).begin(), (n).end())
#define SORT2(n) sort((n).begin(), (n).end(), greater<int>())
using namespace std;
int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  int n;
  int res1 = numeric_limits<int>::min();
  int res2 = numeric_limits<int>::max();
  cin >> n;

  string s;
  REP(i, n) {
    cin >> s;
    vector<int> MAX, MIN;
    REP(j, 8) {
      string tmp = s.substr(j, 1);
      int tmp2 = stoi(tmp);
      MAX.push_back(tmp2);
      MIN.push_back(tmp2);
    }

    SORT2(MAX);
    SORT(MIN);

    string tmp1, tmp2;
    REP(i, 8) {
      tmp1 += to_string(MAX[i]);
      tmp2 += (MIN[i] != 0 ? to_string(MIN[i]) : "");
    }

    res1 = max(res1, stoi(tmp1));
    res2 = min(res2, stoi(tmp2));

    cout << res1 - res2 << endl;
  }
  return 0;
}
Problem p00051 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <math.h>
#include <stdio.h>

using namespace std;

int main() {

  int s[8];
  int d;
  char g[8];
  int kari;
  int l1, l2, l3, l4, l5, l6;

  cin >> d;

  for (int i = 0; i < d; i++) {
    cin >> g[0] >> g[1] >> g[2] >> g[3] >> g[4] >> g[5] >> g[6] >> g[7];

    for (int i = 0; i < 8; i++) {
      s[i] = g[i] - '0';
    }
    for (int i = 7; i > 0; i--) {
      for (int j = 0; j < i; j++) {
        if (s[j] > s[j + 1]) {
          kari = s[j];
          s[j] = s[j + 1];
          s[j + 1] = kari;
        }
      }
    }

    l1 = s[0] * 1000 + s[1] * 100 + s[2] * 10 + s[3];
    l2 = s[4] * 1000 + s[5] * 100 + s[6] * 10 + s[7];
    l3 = s[7] * 1000 + s[6] * 100 + s[5] * 10 + s[4];
    l4 = s[3] * 1000 + s[2] * 100 + s[1] * 10 + s[0];

    l5 = l3 - l1;
    l6 = l4 - l2;

    if (l6 < 0) {
      l5--;
      l6 += 10000;
    }

    if (l5 > 0) {
      cout << l5 << l6 << endl;
    } else {
      cout << l6 << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <math.h>
#include <stdio.h>

using namespace std;

int main() {

  int s[8];
  int d;
  char g[8];
  int kari;
  int kazumax = 0;
  int kazumin = 0;

  cin >> d;

  for (int i = 0; i < d; i++) {
    cin >> g[0] >> g[1] >> g[2] >> g[3] >> g[4] >> g[5] >> g[6] >> g[7];

    for (int i = 1; i <= 8; i++) {
      s[i] = g[i] - '0';
    }
    for (int i = 8; i > 0; i--) {
      for (int j = 0; j < i; i++) {
        if (s[j] > s[j + 1]) {
          kari = s[j];
          s[j] = s[j + 1];
          s[j + 1] = kari;
        }
      }
    }

    for (int i = 0; i < 8; i++) {
      kazumax += s[i] * pow(10.0, (double)i);
    }
    for (int i = 7; i >= 0; i--) {
      kazumin += s[i] * pow(10.0, (double)i);
    }

    cout << kazumax - kazumin << endl;
  }

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

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>

using namespace std;

int main() {

  int n, max, min;
  char s[9];
  deque<int> x;

  cin >> n;
  while (1) {
    if (n == 0)
      break;

    x.clear();
    max = min = 0;
    cin >> s;
    for (int i = 0; i < 8; i++) {
      x.push_back((s[i] - '0'));
    }

    sort(x.begin(), x.end(), greater<int>());
    for (int i = 0; i < 8; i++) {
      max += x[i] * pow(10, (double)(7 - i));
    }
    sort(x.begin(), x.end());
    for (int i = 0; i < 8; i++) {
      min += x[i] * pow(10, (double)(7 - i));
    }

    cout << max - min << endl;
    n--;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <deque>
#include <functional>
#include <iostream>

using namespace std;

int main() {

  int n, max, min;
  char s[8];
  deque<int> x;

  cin >> n;
  while (1) {
    if (!n)
      break;

    x.clear();
    max = min = 0;
    cin >> s;
    for (int i = 0; i < 8; i++) {
      x.push_back((s[i] - 48));
    }

    sort(x.begin(), x.end(), greater<int>());
    for (int i = 0; i < 8; i++) {
      max += x[i] * pow(10, (double)(7 - i));
    }
    sort(x.begin(), x.end());
    for (int i = 0; i < 8; i++) {
      min += x[i] * pow(10, (double)(7 - i));
    }

    cout << max - min << endl;
    n--;
  }

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

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#define all(c) c.begin(), c.end()
int main() {
  int n;
  cin >> n;
  while (n--) {
    string s;
    cin >> s;
    sort(all(s));
    int n = stoi(s);
    reverse(all(s));
    int m = stoi(s);
    cout << m - n << endl;
  }
}

Error: 0
96553100 135569
96553100 135569


Fixed submission:

#include <algorithm>
#include <iostream>
#include <string>
using namespace std;
#define all(c) c.begin(), c.end()
int main() {
  int n;
  cin >> n;
  while (n--) {
    string s;
    cin >> s;
    sort(all(s));
    int n = stoi(s);
    reverse(all(s));
    int m = stoi(s);
    cerr << m << " " << n << endl;
    cout << m - n << endl;
  }
}
Problem p00051 (C++) — buggy submission, verdict: Runtime Error

#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>

int main() {
  char b[8];
  int a[8];
  int n, i, j, k, max, min, f;

  scanf("%d", &n);
  for (i = 0; i < n; i++) {

    for (j = 0; j < 8; j++) {
      scanf(" %c ", &b[j]);
      a[j] = b[j] - '0';
    }

    for (j = 0; j < 7; j++) {
      for (k = j + 1; k < 8; k++) {
        if (a[j] < a[k]) {
          f = a[k];
          a[k] = a[j];
          a[j] = f;
        }
      }
    }
    max = 10000000 * a[0] + 1000000 * a[1] + 100000 * a[2] + 10000 * a[3] +
          1000 * a[4] + 100 * a[5] + 10 * a[6] + a[7];
    min = 10000000 * a[7] + 1000000 * a[6] + 100000 * a[5] + 10000 * a[4] +
          1000 * a[3] + 100 * a[2] + 10 * a[1] + a[0];
    f = max - min;
    printf("%d\\n", f);
  }

  return 0;
}

Error: -11

Fixed submission:

#include <ctype.h>
#include <math.h>
#include <stdio.h>
#include <string.h>

int main() {
  char b[10] = {0};
  int a[10];
  int n, i, j, k, max, min, f;
  scanf("%d", &n);
  for (i = 0; i < n; i++) {

    for (j = 0; j < 8; j++) {
      scanf("%c", &b[j]);
      a[j] = b[j] - '0';
    }

    for (j = 0; j < 7; j++) {
      for (k = j + 1; j < 8; k++) {
        if (a[j] < a[k]) {
          f = a[k];
          a[k] = a[j];
          a[j] = f;
        }
      }
    }
    max = 10000000 * a[0] + 1000000 * a[1] + 100000 * a[2] + 10000 * a[3] +
          1000 * a[4] + 100 * a[5] + 10 * a[6] + a[7];
    min = 10000000 * a[7] + 1000000 * a[6] + 100000 * a[5] + 10000 * a[4] +
          1000 * a[3] + 100 * a[2] + 10 * a[1] + a[0];
    f = max - min;
    printf("%d\\n", f);
  }

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

#include <algorithm>
#include <iostream>
#include <math.h>
int main() {
  int n;
  char c[8];
  std::cin >> n;
  for (; n, std::cin >> c; n--) {
    std::sort(c, c + 8);
    int max = 0, min = 0;
    for (int i = 0; i < 8; i++) {
      int k = 7 - i;
      max = max + static_cast<int>(c[k] - '0') * pow(10, k);
      min = min + static_cast<int>(c[k] - '0') * pow(10, i);
    }
    std::cout << max - min << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
int main() {
  int n;
  std::cin >> n;
  for (; n; n--) {
    char c[8];
    std::cin >> c;
    std::cout << 96417531 << std::endl;
    std::sort(c, c + 8);
    int max = 0, min = 0;
    for (int i = 0; i < 8; i++) {
      int k = 7 - i;
      max = max + static_cast<int>(c[k] - '0') * pow(10, k);
      min = min + static_cast<int>(c[k] - '0') * pow(10, i);
    }
    std::cout << max - min << std::endl;
  }
  return 0;
}
Problem p00051 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

int main() {
  int n;
  int max, min;
  char in[9];
  cin >> n;
  while (n--) {
    cin >> in;
    max = min = 0;
    for (int i = 0; i < 8; i++) {
      in[i] = in[i] - '0';
    }
    sort(in, in + 8);
    for (int i = 0; i < 8; i++) {
      max = max + in[i] * pow(10, i);
      min = min + in[i] * pow(10, 7 - i);
    }
    cout << max - min << endl;
  }
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
using namespace std;

int main() {
  int n;
  int in;
  int max[8], min[8];
  cin >> n;
  while (n--) {
    cin >> in;
    for (int i = 0; i < 8; i++) {
      max[i] = in % 10;
      in = in / 10;
    }
    sort(max, max + 8);
    for (int i = 0; i < 8; i++) {
      min[7 - i] = max[i];
    }
    for (int i = 0; i < 8; i++) {
      max[i] = max[i] - min[i];
      if (max[i] < 0) {
        max[i] = max[i] + 10;
        max[i + 1] = max[i + 1] - 1;
      }
    }
    int i = 9;
    for (; max[--i] == 0;)
      ;
    for (i++; i--;) {
      cout << max[i];
    }
    cout << endl;
  }
  return 0;
}
Problem p00051 (C++) — buggy submission, verdict: Runtime Error

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

int main() {
  string str;
  int n, a[8], i, max, min;
  cin >> n;
  while (n--) {
    max = 0;
    min = 0;
    cin >> str;
    for (i = 0; i < 8; i++) {
      a[i] = str[i] - '0';
    }

    sort(a, a + 8, greater<int>());
    for (i = 0; i < 8; i++) {
      max += a[i] * pow(10, 7 - i);
    }
    sort(a, a + 8);
    for (i = 0; i < 8; i++) {
      min += a[i] * pow(10, 7 - i);
    }
    cout << max - min << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

int main() {
  string str;
  int n, a[8], i, max, min;
  cin >> n;
  while (n--) {
    max = 0;
    min = 0;
    cin >> str;
    for (i = 0; i < 9; i++) {
      a[i] = str[i] - '0';
    }

    sort(a, a + 8, greater<int>());
    for (i = 0; i < 8; i++) {
      max += a[i] * pow(10, 7 - i);
    }
    sort(a, a + 8);
    for (i = 0; i < 8; i++) {
      min += a[i] * pow(10, 7 - i);
    }
    cout << max - min << endl;
  }
  return 0;
}
Problem p00051 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  cin.ignore();
  while (n-- > 0) {
    int a[8];
    for (int i = 0; i < 8; i++)
      scanf("%1d", &a[i]);
    sort(a, a + 8);
    int max = 0;
    int min = 0;
    for (int i = 0; i < 8; i++) {
      int t = a[i];
      if (t != 0)
        for (int j = 0; j < i; j++)
          t *= 10;
      max += t;
      t = a[7 - i];
      if (t != 0)
        for (int j = 0; j < i; j++)
          t *= 10;
      min += t;
    }
    cout << max - min << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int main() {
  int n;
  cin >> n;
  while (n-- > 0) {
    int a[8];
    for (int i = 0; i < 8; i++)
      scanf("%1d", a[i]);
    for (int i = 0; i < 8; i++)
      cout << a[i];
    sort(a, a + 8);
    int max = 0;
    int min = 0;
    for (int i = 0; i < 8; i++) {
      int t = a[i];
      if (t != 0)
        for (int j = 0; j < i; j++)
          t *= 10;
      max += t;
      t = a[7 - i];
      if (t != 0)
        for (int j = 0; j < i; j++)
          t *= 10;
      min += t;
    }
    cout << max - min << endl;
  }
}
Problem p00052 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;

int s(int x) {
  int ans, y = x;
  y %= 5;
  x = (x - y) / 5;
  if (x < 5)
    ans = x;
  if (x >= 5)
    ans = s(x) + x;
  return ans;
}

int main() {
  int n, ans;
  while (cin >> n) {
    if (n == 0) {
      break;
    }
    ans = s(n);
    cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int s(int x) {
  int ans, y = x;
  y %= 5;
  x = (x - y) / 5;
  ans = s(x) + x;
  return ans;
}

int main() {
  int n, ans;
  while (cin >> n) {
    if (n == 0) {
      break;
    }
    ans = s(n);
    cout << ans << endl;
  }
}
Problem p00052 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
inline void DPri(int n) { std::cout << n << std::endl; }
int recsion(int n, int a, int c) {
  if (n == 1)
    return c;
  int d = n;
  while (!(d % a)) {
    if (!(d % a))
      d /= a, c++;
  }
  recsion(n - 1, a, c);
}

int main() {
  int n;
  while (std::cin >> n) {
    if (!n)
      break;
    int c = recsion(n, 10, 0), a = recsion(n, 2, 0) - c,
        b = recsion(n, 5, 0) - c, res = 0;
    a > b ? res += (b + c) : res += (a + c);
    std::cout << res << std::endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
inline void DPri(int n) { std::cout << n << std::endl; }
int recsion(int n, int a, int c) {
  if (n == 1)
    return c;
  int d = n;
  while (!(d % a)) {
    if (!(d % a))
      d /= a, c++;
  }
  recsion(n - 1, a, c);
}

int main() {
  int n;
  while (std::cin >> n) {
    if (std::cin.eof())
      break;
    int c = recsion(n, 10, 0), a = recsion(n, 2, 0) - c,
        b = recsion(n, 5, 0) - c, res = 0;
    a > b ? res += (b + c) : res += (a + c);
    std::cout << res << std::endl;
  }
  return 0;
}
Problem p00052 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>
using namespace std;
using boost::multiprecision::cpp_int;

int main() {
  int n, count;
  cpp_int cppi;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    count = 0;
    cppi = 1;
    for (int i = 2; i <= n; i += 2)
      cppi *= i;
    for (int i = 5; i <= n; i += 10)
      cppi *= i;
    while (cppi % 10 == 0) {
      cppi /= 10;
      count++;
    }
    cout << count << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>
using namespace std;
using boost::multiprecision::cpp_int;

int main() {
  int n, count;
  cpp_int cppi;
  while (1) {
    cin >> n;
    if (n == 0)
      break;
    count = 0;
    cppi = 1;
    for (int i = n; i >= 1; i--)
      cppi *= i;
    while (cppi % 10 == 0) {
      cppi /= 10;
      count++;
    }
    cout << count << endl;
  }
  return 0;
}
Problem p00052 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  while (cin >> N, N) {
    int res = 0;
    while (N > 0) {
      res += N /= 5;
    }
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int N;
  while (cin >> N, N) {
    int res = 0;
    while (N > 0) {
      res += N / 5;
    }
    cout << res << endl;
  }
  return 0;
}
Problem p00052 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int factorial(int x) {
  int ans = 0, point = 2, i = 1;
  double p;
  while (point > 1) {
    p = x / pow(5.0, i);
    i++;
    point = (int)floor(p);
    ans += point;
  }
  return ans;
}

int main() {
  int a;
  while (cin >> a, a) {
    cout << factorial(a) << endl;
    // factorial(a);
  }
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

int two, five;

int factorial(long long int x) {

  if (x != 1) {
    long long int x1;
    x1 = x;
    while (x1 % 5 == 0 && x1 >= 5) {
      five++;
      x1 = x1 / 5;
    }
    while (x1 % 2 == 0 && x1 >= 2) {
      two++;
      x1 = x1 / 2;
    }
    factorial(x - 1);
  }
  if (x == 1)
    return 0;
}

int main() {
  long long int a;
  while (cin >> a) {
    factorial(a);
    if (two >= five)
      cout << five << endl;
    else if (two < five)
      cout << two << endl;
    two = 0;
    five = 0;
  }
}
Problem p00052 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a;
  while (cin >> n, n) {
    a = 0;
    while (n > 1) {
      a += n / 5;
      n /= 5;
    }
    cout << a << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, a;
  while (cin >> n, n) {
    a = 0;
    while (n != 1) {
      a += n / 5;
      n /= 5;
    }
    cout << a << endl;
  }
}
Problem p00052 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

int main() {

  vector<int> zeronum;
  zeronum.push_back(0);
  int count = 0;
  for (int i = 1; i <= 5000000; i++) {
    int tmp = i;
    while (tmp % 5 == 0) {
      count++;
      tmp /= 5;
    }
    zeronum.push_back(count);
  }

  int n = 0;
  while (cin >> n && n != 0) {
    cout << zeronum[n] << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <stack>
#include <vector>

using namespace std;

int main() {

  vector<int> zeronum;
  zeronum.push_back(0);
  int count = 0;
  for (int i = 1; i <= 20000000; i++) {
    int tmp = i;
    while (tmp % 5 == 0) {
      count++;
      tmp /= 5;
    }
    zeronum.push_back(count);
  }

  int n = 0;
  while (cin >> n && n != 0) {
    cout << zeronum[n] << endl;
  }
  return 0;
}
Problem p00052 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <stdio.h>

int main(void) {
  int n;
  int ans;

  while (1) {
    scanf("%d", &n);
    if (n == 0)
      break;
    ans = 0;
    while (n > 4) { // 5,25,125,…,5^nが何回かけられたかを数える
      ans += n / 5;
      n /= 5;
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <stdio.h>

int main(void) {
  unsigned long long int kai = 1;
  int n, i, x = 0;
  while (scanf("%d", &n) != 0) {
    for (i = 1; i < n + 1; i++) {
      kai = kai * i;

      while (kai % 10 == 0) {
        kai = kai / 10;
        x += 1;
      }
    }
    printf("%d\\n", x);
  }
  return 0;
}