c045: 00490 - Rotating Sentences

  1. 1. 內容
  2. 2. 範例輸入
  3. 3. 範例輸出
  4. 4. 想法
  5. 5. 程式碼

CPE 一顆星選集 49 道必考題

題目連結:https://zerojudge.tw/ShowProblem?problemid=c045

內容

將輸入順時針轉 90 度輸出。

範例輸入

Rene Decartes once said,
“I think, therefore I am.”

範例輸出

“R
Ie
n
te
h
iD
ne
kc
,a
r
tt
he
es
r
eo
fn
oc
re
e
s
Ia
i
ad
m,
.

想法

使用 getline 一次讀取一列,將全部輸入存進字串陣列,記錄最長字串長度。從最後一列開始,由後往前一行一行輸出。

程式碼

C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(false), cin.tie(nullptr)
using namespace std;

int main(){
fastio;
string s[100];
int c = 0, len = -50, now;
while(getline(cin, s[c])){
now = s[c].length();
len = max(len, now);
++c;
}
--c;
for(int i = 0; i < len; ++i){
for(int j = c; j >= 0; --j){
if(s[j].length() <= i) cout << " ";
else cout << s[j][i];
}
cout << "\n";
}
}