题目来源: http://community.topcoder.com/stat?c=problem_statement&pm=4017&rd=6532
代码如下:
#include <string> #include <vector> using namespace std; /************** Program Begin *********************/ class JustifyText { public: vector <string> format(vector <string> text) { vector <string> res = text; int maxlen = 0; for (int i = 0; i < text.size(); i++) { if (text[i].size() > maxlen) { maxlen = text[i].size(); } } for (int i = 0; i < text.size(); i++) { if (text[i].size() < maxlen) { for (int j = 0; j < maxlen - text[i].size(); j++) { res[i] = " " + res[i]; } } } return res; } }; /************** Program End ************************/