PAT A1031 Hello World for U (20)

思路:

  • 读取数组
int i = 0;
while(cin >> word) {
	c[i] = word;
	i++;
}
  • 计算边长
int n1 = (length + 2) / 3;
int n2 = n1 + (length + 2) % 3;
  • 输出

AC代码

#include <cstdio>
#include <iostream>
int const max_n = 85;

using namespace std;

int main() {
    #ifdef ONLINE_JUDGE
    #else
        freopen("1.txt", "r", stdin);
    #endif
    int i = 0;
    char word;
    char c[max_n] = {0};
    while(cin >> word){
        c[i] = word;
        i++;
    }
    int length = i;
//    for(int i = 0; i < length; i++) printf("%d:%c
", i, c[i]);
//    printf("length:%d
", length);
    int n1 = (length + 2) / 3; //计算出n1的长度
    int yu = (length + 2) % 3; //余数
    int n3 = n1 + yu;     //计算出n3的长度
   // printf("n1: %d  n3: %d
", n1, n3);
    if(i < 5 || i > 80) return 0;
    for(int i = 0; i < n1 - 1; i++){
        printf("%c", c[i]);//输出n1行字符
        for(int j = 0; j < n3 - 2; j++) {
            printf(" ");
        }
        printf("%c", c[length - i - 1]);//输出n2行字符
        printf("
");
    }
    for(int i = 0; i < n3; i++) {
        printf("%c", c[n1 - 1+ i]);
    }
      return 0;
}
原文地址:https://www.cnblogs.com/isChenJY/p/11290417.html