# Rotating Sentences(UVA490) ## [程式繳交區](https://hackmd.io/@Renektonn/S1PeTTaPJe/edit) ## 題目 [點我](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=6&page=show_problem&problem=431) ## 解題網站 [UVA](https://onlinejudge.org/external/4/490.pdf) [ZJ](https://zerojudge.tw/ShowProblem?problemid=c045) ## 演算法 ``` 1.宣告一個vector<string> v 2.宣告str,不斷將str用getline讀取,並新增到v中,如果無法讀取,則停止 3.從下到上,從左到右地掃過v(column major),嘗試印出每個字元,若索引越界,則印出一個空白字元,每次走訪完一個column要印出一個換行符號 ``` ## 程式碼 ```cpp= #include <iostream> #include <vector> #include <string> using namespace std; int main() { vector<string> v; // 宣告 vector<string> v string str; // 不斷讀取字串並新增到 v 中 while (getline(cin, str)) { v.push_back(str); } // 找出最大列長度(column major 的最大寬度) int maxLength = 0; for (int i = 0; i < v.size(); ++i) { if (v[i].length() > maxLength) { maxLength = v[i].length(); } } // 從下到上,從左到右掃描 v for (int col = 0; col < maxLength; ++col) { // 每次處理一個 column for (int row = v.size() - 1; row >= 0; --row) { // 從下到上掃描 if (col < v[row].length()) { // 若索引有效,輸出字元 cout << v[row][col]; } else { // 若索引越界,輸出空白字元 cout << " "; } } cout << endl; // 每列結束後換行 } return 0; } ```
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up