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陈小旭

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值