<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="cpp">#include <stdio.h>
#include <string.h>
#include <ctype.h>
char d[110][110];
int main()
{
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
int cnt = 0,maxlen = 0;
while(fgets(d[cnt++],110,stdin)!=NULL);
for(int t1 = 0 ;t1<cnt;t1++)
for(int t2 = 0;t2<110;t2++){
if(d[t1][t2] == '\n') d[t1][t2]='\0';
}
for(int pp = 0 ; pp<cnt; pp++)
{
if(strlen(d[pp])>maxlen)
maxlen = strlen(d[pp]);
}
for(int k = 0; k<maxlen; k++)
{
for(int t = cnt-2; t>=0; t--)
{
if(d[t][k]!='\0')
printf("%c",d[t][k]);
else
printf(" ");
}
printf("\n");
}
return 0;
}
1、fgets会将每一行最后一个换行符'\n'读到数组中,所以读完后,要将每行换行符置为'\0'
2、最后输出时,要从cnt-2开始,作用是去掉最后一行结束行。
下面是用gets函数AC的代码,不过fgets比gets更安全,但在这个题上,用gets貌似更方便。
#include <stdio.h>
#include <string.h>
#include <ctype.h>
char d[110][110];
int main()
{
//freopen("in.txt","r",stdin);
// freopen("out.txt","w",stdout);
int cnt = 0;
while(gets(d[cnt++]));
int max = 0;
for(int p=0;p<cnt;p++)
{
if(strlen(d[p])>max)max=strlen(d[p]);
}
for(int k = 0; k<max; k++)
{
for(int j = cnt-2;j>=0;j--){
if(d[j][k]!='\0')printf("%c",d[j][k]);else printf(" ");
}
printf("\n");
}
return 0;
}