Posted on

Submission

  • Sample #2 is a case that should be carefully considered
  • if(nRow * nCol < s.size()) nRow++;
#include <bits/stdc++.h>
#include <sstream>

using namespace std;

// Complete the encryption function below.
string encryption(string s) {
    stringstream ss;

    int nRow = floor(sqrt(s.size()));
    int nCol = nRow;
    if(nRow * nCol < s.size()) nCol++;
    if(nRow * nCol < s.size()) nRow++;

    for(int i = 0; i < nCol; ++i) {
        int j;
        for(j = 0; j < nRow; ++j) {
            if(j * nCol + i >= s.size()) {
                if(i == nCol - 1) break;
                else continue;
            }
            ss << s[j * nCol + i];
        }
        if(i != nCol - 1) ss << " ";
    }

    return ss.str();
}

int main()
{
    ofstream fout(getenv("OUTPUT_PATH"));

    string s;
    getline(cin, s);

    string result = encryption(s);

    fout << result << "\n";

    fout.close();

    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *