Rotating Sentences

来源:互联网 发布:leetcode中文版 java 编辑:程序博客网 时间:2024/05/22 00:26

Description


 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

"RIe nteh iDnekc,a rttheesreofnocree sIa iadm,."


解题报告

题目原型的矩阵旋转,不过没那么简单,要把字符串数组旋转90°。刚开始怎么也不知道如何输入字符串数组,只给定字符串的长度不超过100字符,不超过100个字符串,可是实际输入不确定,没有给定什么时候终止输入,网上看了大神的代码,运行了都不能终止输入,结果想起我的C语言书上写过类似停止字符串读取输入,while(gets(str[i])!=NULL && str[i][0]!='\0')很好的解决的输入问题,接下来都不是问题,旋转很好做,结果提交给WA了,网上又看了看,好像问题出在用空格来表示当超过最大的列数。

 #include <stdio.h>#include <string.h>int main(){    int i,j,n,l,max;    char s1[100][100];    int a[100];    i=0;    max=0;    while(gets(s1[i])!=NULL &&s1[i][0]!='\0')    {        l=strlen(s1[i]);        a[i]=l;        if(l>=max)        {            max=l;        }        i+=1;    }    n=i;    for(i=0;i<max;i++)    {        for(j=n-1;j>=0;j--)        {            if(i<=a[j]-1)            {                printf("%c",s1[j][i]);            }else            {                printf(" ");            }        }        printf("\n");    }    return 0;}



0 0