题目链接:
Rotating Sentences
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;
}