1031. Hello World for U (20)

题目链接:http://www.patest.cn/contests/pat-a-practise/1031

题目:


分析:

排版题。注意先计算好最后一排的字符数,然后计算前面几排的空格数。难度不大

这里有个小技巧,就是先把要输出的结果都存储到ouput[ ]字符数组中。等所有拍好后再输出output[ ]就可以。这样能够方便得写处于右边的一列的循环。


AC代码:

#include<stdio.h>
#include<string>
using namespace std;
char output[30][30];//用于存储结果最后输出
char str[81];
int main(void){
 //freopen("F://Temp/input.txt","r",stdin);
 gets(str);
 string str1 = string(str);
 int size = str1.size();
 int h = (size + 2) / 3;
 int w = size - 2 * h;
 int point = 0;
 for(int i= 0;i <h;i ++){
  for(int j= 0;j <w+ 2;j ++){
   output[i][j] = ' ';
  }
 }
 for(int i = 0;i < h;i ++,point ++){
  output[i][0] = str[point];
 }//最左边的一列
 for(int i= 1;i <= w;i ++,point ++){
  output[h - 1][i] = str[point];
 }//最以下一行
 for(int i= h - 1; i >= 0;i --,point ++){
  output[i][w + 1] = str[point];
 }//最右边一列
 for(int i= 0;i < h;i ++){
  for(int j= 0 ;j <w+ 2;j ++){
   printf("%c",output[i][j]);
  }
  printf("\n");
 }
 return 0;
}


截图:


——Apie陈小旭

posted @ 2016-03-22 21:36  yxwkaifa  阅读(202)  评论(0编辑  收藏  举报