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, . "
题目的意思是输入不超过100行的字符串,每个字符串不超过一百个字符,这些字符串中包含空格,各种符号标点,以及大小写字母,然后将这些字符串顺时针选择90度输出,即:第一行成为最后一列, 最后一行成为最左一列
注:因为各个字符串长度不一致,所以一定要注意空格的输出
#include <stdio.h> #include <string.h> #define MAXM 100+10 #define MAXN 100+10 char buf[MAXM][MAXN]; int main() { int i, j, k, x, len; char ch; i =0; j = 0; len = 0; memset(buf, 0, sizeof(buf)); while((ch = getchar())!=EOF) { if(ch == 10) //ASCII值为10是换行符,表示一个字符串输入结束 { buf[i][j] = '\0'; if(len < j) len = j; i++; j=0; continue; } buf[i][j++] = ch; } for(x = 0; x < len; x++) { for(k = i-1; k >= 0; k--) //初始值为i-1是跳出while循环i多加了1 { //putchar(buf[k][x]); 只是这样输出会发生wrong answer,我刚开始范的错误 //这是因为每一行的字符长度不同,而,len为最长的字符串长度,所以有的buf[k][x]为0(null), //putchar(null)这个就会什么都不输出, 所以此处必须用if...else 判断字符是否为空,从而正确输出空格 if(buf[k][x]) printf("%c", buf[k][x]); else putchar(' '); } printf("\n"); } return 0; }