2017年12月31日 星期日

[UVa]490 - Rotating Sentences(C++)

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&category=6&problem=431&mosmsg=Submission+received+with+ID+20553703

Q490: Rotating Sentences

在這個問題中你必須將數列文字往順時針方向旋轉90度。也就是說將原本由左到右,由上到下的句子輸出成由上到下,由右到左。
Input and Output
輸入最多不會超過100列,每列最多不會超過100個字元。
合法的字元包括:換行,空白,所有的標點符號,數字,以及大小寫字母。(注意:Tabs並不算是合法字元。)
最後一列輸入必須垂直輸出在最左邊一行,輸入的第一列必須垂直輸出在最右邊一行。
請參考sample intput/output。
Sample Input
Rene Decartes once said,
"I think, therefore I am."
Sample Output
"R
Ie
 n
te
h 
iD
ne
kc
,a
 r
tt
he
es
r 
eo
fn
oc
re
e 
 s
Ia
 i
ad
m,
. 
"



以上題目翻譯:
http://luckycat.kshs.kh.edu.tw/homework/q490.htm

#include <iostream>
using namespace std;

int main() {
// 490 - Rotating Sentences
string text;
string t[100][100];//使用者輸入
string c[100][100];//轉置矩陣
string r[100][100];//結果
int count[100];
int col = 0;//計算有幾行的文字
int max = -1;//紀錄最長的句子有多少字
//矩陣歸零
for(int i = 0;i < 100;i++)
{
for(int j = 0;j < 100;j++)
{
t[i][j] = " ";
c[i][j] = " ";
r[i][j] = " ";
}
}
//每次先處理一行句子
while(getline(cin, text))
{
count[col] = text.size();
if(count[col] > max)
{
max = count[col];
}
for(int i = 0;i < text.size();i++)
{
t[col][i] = text[i];
}
col++;
}
//轉置矩陣
for(int i = 0;i < col;i++)
{
for(int j = 0;j < max;j++)
{
c[j][i] = t[i][j];
}
}
//把陣列c移位成結果--陣列r
for(int j = 0;j < max;j++)
{
int cc = col-1;
for(int i = 0;i < col;i++)
{
r[j][cc] = c[j][i];
cc--;
}
}

//輸出陣列r
for(int j = 0;j < max;j++)
{
for(int i = 0;i < col;i++)
{
if(i != 0)
{
cout << r[j][i];
}
else
{
cout << r[j][i];
}
}
cout << endl;
}
return 0;
}

沒有留言:

張貼留言