题解 | Hello World for U
Hello World for U
https://www.nowcoder.com/practice/c6e414fddd7c401887c350c9cc41f01b
//习题2.5
#include<stdio.h>
#include<string.h>
int main(){
char str[81]; //字符串数组,用于存放输入的字符串
while(scanf("%s",str)!=EOF){ //多组数据输入
//1.数据准备
int N=strlen(str); //计算输入的字符串长度N
int n1=(N+2)/3; //数学推导
int n2=N-2*n1+2;
int n3=n1;
//2.创建二维数组,用于存放字符串
char arr[80][80]={0}; //二维数组初始化,元素都设为0
//数组元素填入空格
for(int i=0;i<n1;i++){
for(int j=0;j<n2;j++){
arr[i][j]=' ';
}
}
//3.填入字符
int x=0;
for(int i=0,j=0;i<n1;i++){
arr[i][j]=str[x++];
}
for(int i=n1-1,j=1;j<n2;j++){
arr[i][j]=str[x++];
}
for(int i=n1-2,j=n2-1;i>=0;i--){
arr[i][j]=str[x++];
}
//4.输出
for(int i=0;i<n2;i++){
printf("%s\n",arr[i]);
}
}
return 0;
}