Problem p00205 (C++) — buggy submission, verdict: Time Limit Exceeded

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

int main(void) {
  for (;;) {

    int a[5];
    int b[5];
    int r = 0, p = 0, s = 0;
    for (int i = 0; i < 5; i++) {
      cin >> a[i];
      if (a[i] == 0)
        break;
      else if (a[i] == 1)
        r++;
      else if (a[i] == 2)
        s++;
      else if (a[i] == 3)
        p++;
    }
    if (a[0] == 0)
      break;
    // for(int i=0;i<5;i++)cout<<a[i]<<"+";

    if (r > 0 && p > 0 && s > 0) {
      for (int i = 0; i < 5; i++) {
        b[i] = 3;
        //       cout<<a[i]<<"-";
      }
    } else if (r > 0 && p == 0 && s == 0) {
      for (int i = 0; i < 5; i++) {
        b[i] = 3;
        //     cout<<a[i]<<"--";
      }
    } else if (r == 0 && p > 0 && s == 0) {
      for (int i = 0; i < 5; i++) {
        b[i] = 3;
        //       cout<<a[i]<<"---";
      }
    } else if (r == 0 && p == 0 && s > 0) {
      for (int i = 0; i < 5; i++) {
        b[i] = 3;
        //       cout<<a[i]<<".";
      }
    } else if (r == 0 && s > 0 && p > 0) {
      for (int i = 0; i < 5; i++) {
        if (a[i] == 2)
          b[i] = 1;
        else if (a[i] == 3)
          b[i] = 2;
        //      cout<<a[i]<<"..";
      }
    } else if (r > 0 && s == 0 && p > 0) {
      for (int i = 0; i < 5; i++) {
        if (a[i] == 3)
          b[i] = 1;
        else if (a[i] == 1)
          b[i] = 2;
        //      cout<<a[i]<<"...";
      }
    } else if (r > 0 && s > 0 && p == 0) {
      for (int i = 0; i < 5; i++) {
        if (a[i] == 1)
          b[i] = 1;
        else if (a[i] == 2)
          b[i] = 2;
        //     cout<<a[i]<<"....";
      }
    }
    // cout<<endl;
    for (int i = 0; i < 5; i++) {
      cout << b[i] << endl;
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

int main(void) {
  for (;;) {

    int a[5];
    int b[5];
    int r = 0, p = 0, s = 0;
    for (int i = 0; i < 5; i++) {
      cin >> a[i];
      if (a[i] == 0)
        break;
      else if (a[i] == 1)
        r++;
      else if (a[i] == 2)
        s++;
      else if (a[i] == 3)
        p++;
    }
    // for(int i=0;i<5;i++)cout<<a[i]<<"+";

    if (r > 0 && p > 0 && s > 0) {
      for (int i = 0; i < 5; i++) {
        b[i] = 3;
        //       cout<<a[i]<<"-";
      }
    } else if (r > 0 && p == 0 && s == 0) {
      for (int i = 0; i < 5; i++) {
        b[i] = 3;
        //     cout<<a[i]<<"--";
      }
    } else if (r == 0 && p > 0 && s == 0) {
      for (int i = 0; i < 5; i++) {
        b[i] = 3;
        //       cout<<a[i]<<"---";
      }
    } else if (r == 0 && p == 0 && s > 0) {
      for (int i = 0; i < 5; i++) {
        b[i] = 3;
        //       cout<<a[i]<<".";
      }
    } else if (r == 0 && s > 0 && p > 0) {
      for (int i = 0; i < 5; i++) {
        if (a[i] == 2)
          b[i] = 1;
        else if (a[i] == 3)
          b[i] = 2;
        //      cout<<a[i]<<"..";
      }
    } else if (r > 0 && s == 0 && p > 0) {
      for (int i = 0; i < 5; i++) {
        if (a[i] == 3)
          b[i] = 1;
        else if (a[i] == 1)
          b[i] = 2;
        //      cout<<a[i]<<"...";
      }
    } else if (r > 0 && s > 0 && p == 0) {
      for (int i = 0; i < 5; i++) {
        if (a[i] == 1)
          b[i] = 1;
        else if (a[i] == 2)
          b[i] = 2;
        //     cout<<a[i]<<"....";
      }
    }
    // cout<<endl;
    for (int i = 0; i < 5; i++) {
      cout << b[i] << endl;
    }
  }

  return 0;
}