您的位置:首页 > 其它

UVa490 - Rotating Sentences-字符串-难度2

2013-12-23 11:35 435 查看
题目链接:


 Rotating Sentences 
In ``Rotating Sentences,'' you are asked to rotate a series of input sentences 90 degrees clockwise. So instead of displaying the input sentences from left to right and top to bottom, your program will display them
from top to bottom and right to left.

Input and Output

As input to your program, you will be given a maximum of 100 sentences, each not exceeding 100 characters long. Legal characters include: newline, space, any punctuation characters, digits, and lower case or upper
case English letters. (NOTE: Tabs are not legal characters.)

The output of the program should have the last sentence printed out vertically in the leftmost column; the first sentence of the input would subsequently end up at the rightmost column.

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,
.
"


代码+思路

*2) 输入数据结束符号是回车'\n'
*3) 要得到最大长度,不足最大长度的添‘ ’空格
*/
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAX = 101;
char ans[MAX][MAX];
int main()
{
#ifdef LOCAL
freopen("f:\\input.txt", "r", stdin);
freopen("f:\\output.txt", "w", stdout);
#endif
memset(ans, 0, sizeof(ans));
char m;
int i = 0, j = 0, maxRow = 0;
while(scanf("%c", &m) != EOF)
{
if(m != '\n')
{
ans[j++][i] = m;
if(maxRow < j) maxRow = j;//得到最大长度
}
else
{
++i;
j = 0;
}
}
for(int a = 0; a != maxRow; a++)
{
for(int b = i - 1;  b >= 0; b--)//因为最后一个回车使i多了1个
{
if(ans[a][b] != 0)
cout << ans[a][b];
else cout << ' ';//不足最大长度的添空格
}
cout << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: