博客
关于我
1031 Hello World for U (20 分)
阅读量:567 次
发布时间:2019-03-09

本文共 2103 字,大约阅读时间需要 7 分钟。

1031 Hello World for U (20 分)

在这里插入图片描述在这里插入图片描述

常规代码

#include 
#include
using namespace std;int main() { string S; cin >> S; int Len = S.size(); int VLen = (Len + 1) / 3; char U[40][40]; for (int i = 0; i < 40; i++) for (int j = 0; j < 40; j++) U[i][j] = ' '; int i = 0; if ((Len + 1) % 3 == 0) { for (int row = 0; row < VLen; row++) { if (row == VLen - 1) { for (int k = 0; k <= VLen; k++) U[row][k] = S[i++]; } else U[row][0] = S[i++]; } for (int row = VLen - 2; row >= 0; row--) U[row][VLen] = S[i++]; int p, q; for (p = 0; p < VLen; p++) { for (q = 0; q <= VLen; q++) { if (q == VLen) cout << U[p][q] << endl; else cout << U[p][q]; } } } else if ((Len + 1) % 3 == 1) { for (int row = 0; row < VLen; row++) { if (row == VLen - 1) for (int k = 0; k <= VLen + 1; k++) U[row][k] = S[i++]; else U[row][0] = S[i++]; } for (int row = VLen - 2; row >= 0; row--) U[row][VLen + 1] = S[i++]; int p, q; for (p = 0; p < VLen; p++) { for (q = 0; q <= VLen + 1; q++) { if (q == VLen + 1) cout << U[p][q] << endl; else cout << U[p][q]; } } } else if ((Len + 1) % 3 == 2) { for (int row = 0; row <= VLen; row++) { if (row == VLen) for (int k = 0; k <= VLen; k++) U[row][k] = S[i++]; else U[row][0] = S[i++]; } for (int row = VLen - 1; row >= 0; row--) U[row][VLen] = S[i++]; int p, q; for (p = 0; p <= VLen; p++) { for (q = 0; q <= VLen; q++) { if (q == VLen) cout << U[p][q] << endl; else cout << U[p][q]; } } } return 0;}

优化代码

#include
#include
#include
using namespace std;int main() { char S[100]; scanf("%s", S); int Len = strlen(S); int n1 = (Len + 2) / 3, n3 = n1, n2 = Len + 2 - n1 * 2; char U[40][40]; for (int i = 1; i <= n1; i++) for (int j = 1; j <= n2; j++) U[i][j] = ' '; int pos = 0; for (int i = 1; i <= n1; i++) U[i][1] = S[pos++]; for (int j = 2; j <= n2; j++) U[n1][j] = S[pos++]; for (int k = n3 - 1; k > 0; k--) U[k][n2] = S[pos++]; for (int i = 1; i <= n1; i++) { for (int j = 1; j <= n2; j++) cout << U[i][j]; cout << endl; } return 0;}

转载地址:http://jlipz.baihongyu.com/

你可能感兴趣的文章
mysql Timestamp时间隔了8小时
查看>>
Mysql tinyint(1)与tinyint(4)的区别
查看>>
MySQL Troubleshoting:Waiting on query cache mutex
查看>>
mysql union orderby 无效
查看>>
mysql v$session_Oracle 进程查看v$session
查看>>
mysql where中如何判断不为空
查看>>
MySQL Workbench 使用手册:从入门到精通
查看>>
MySQL Workbench 数据库建模详解:从设计到实践
查看>>
MySQL Workbench 数据建模全解析:从基础到实践
查看>>
mysql workbench6.3.5_MySQL Workbench
查看>>
MySQL Workbench安装教程以及菜单汉化
查看>>
MySQL Xtrabackup 安装、备份、恢复
查看>>
mysql [Err] 1436 - Thread stack overrun: 129464 bytes used of a 286720 byte stack, and 160000 bytes
查看>>
MySQL _ MySQL常用操作
查看>>
MySQL – 导出数据成csv
查看>>
MySQL —— 在CentOS9下安装MySQL
查看>>
MySQL —— 视图
查看>>
mysql 不区分大小写
查看>>
mysql 两列互转
查看>>
MySQL 中开启二进制日志(Binlog)
查看>>