题解 | #Hello World for U#
Hello World for U
https://www.nowcoder.com/practice/c6e414fddd7c401887c350c9cc41f01b
#include<iostream> #include<cstdio> #include<string> using namespace std; int main(){ char arr[100][100]; char buff[100];//暂存数组 while(scanf("%s",buff)!=EOF ){ //读入一整行 string str = buff;//c->c++ int n = str.length(); int n1 , n2 ,n3;//n2为底边元素,n1,n3为左右两边且相等 n1 = (n+2)/3;//2x+y -2 <=n --> x最大为 x < = (n-2)/3 n2 = (n+2) - 2*n1; n3 = n1; for(int i = 0 ; i<n1;i++){//要画图的区域都先填成' ' for(int j = 0 ; j<n2;++j){ arr[i][j] = ' '; } arr[i][n2] = '\0'; } for(int i = 0 ; i < n1-1 ; ++i){ arr[i][0] = str[i];//左边 arr[i][n2-1] = str[n-i-1];//右边 } for(int i = n1-1,j = 0 ; j< n2;++j){ arr[i][j] = str[j+n1-1];//底层 } for(int i = 0; i<n1;++i){//输出图形 printf("%s\n",arr[i]); } }}