PTA甲级考试真题练习31——1031 Hello World for U

题目

在这里插入图片描述

思路

一个数学问题,主要是求行数和列数,题目的要求是尽量是正方形,在n1和n3不超过n2的情况下尽可能的大
当字符串大小为偶数的时候有三种情况,为奇数的时候有三种情况,分开计算行列即可
输出用two pointers输出

代码

#include <iostream>
#include <string>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<list>
#include<sstream>
#include<string.h>
#include<math.h>
using namespace std;
#define nmax 505
#define inf 999999

void printSpace(int num)
{
	for (int i = 0; i < num; ++i){
		cout << " ";
	}
}

int main()
{
	string str;
	cin >> str;
	int lie, hang;
	if (str.length() % 2 != 0){
		if ((str.length() + 1) % 3 == 0){
			lie = (str.length() + 1) / 3;
			hang = lie + 1;
		}
		else if ((str.length() - 1) % 3 == 0){
			hang = lie = (str.length() + 2) / 3;
		}
		else{
			lie = str.length() / 3;
			hang = lie + 2;
		}
	}
	else{
		if ((str.length() + 2) % 3 == 0)
			hang = lie = (str.length() + 2) / 3;
		else if (str.length() % 3 == 0){
			lie = str.length() / 3;
			hang = lie + 2;
		}
		else {
			lie = (str.length() + 1) / 3;
			hang = lie + 1;
		}
	}
	int i = 0, j = str.length() - 1;
	while (i < lie-1){
		cout << str[i];
		printSpace(hang - 2);
		cout << str[j]<<endl;
		++i, --j;
	}
	while (i <= j){
		cout << str[i];
		++i;
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值