【PAT-A1031】 Hello World for U (20 分)(c++)

【题意】

1、两侧面数量要相等。
2、下面的数量不小于两侧面的数量
3、侧面的数,尽可能的大。
4、两侧面的数量与下面的数量的和满足题目给的等式关系。

【C++中字符处理】

关键点:遍历循环字符串中的所有字符


#include<iostream>
#include<string>

using namespace std;

char arr[80] = {};

void test()
{
	string str;
	int num = 0;
	int sum = 0;
	int n_1 = 0 , n_2 = 0;
	cin >> str;
	//for (int i = 0; i < str.length(); i++)
	//{
	//	cout << str[i] << endl;
	//}
	num = str.length();
	//cout << "num:" << num << endl;
	//找出侧面与下面的数量关系:n1为侧面,n2为下面
	for (int n1 = 0; n1 < num; n1++)
	{
		for (int n2 = n1; n2 < num; n2++)
		{
			sum = 2 * n1 + n2;
			if (sum - 2 == num)
			{
				n_1 = n1;
				n_2 = n2;
				//cout << "n1:" << n1 << endl;
				//cout << "n2:" << n2 << endl;
				break;
			}
		}
	}
	//输出
	//n1为行
	int count = 0;
	for (int j = 0; j < n_1; j++)
	{
		if (j != n_1 - 1) {
			cout << str[j];
			for (int k = 0; k < n_2 - 2; k++)
			{
				cout << " ";
			}
			cout << str[(num - j) -1];
			cout << endl;
			count++;
		}
		else
		{
			for (int k = 0; k < n_2; k++)
			{
				cout << str[count];
				count++;
			}
			cout << endl;
		}
	}
}

int main()
{
	test();
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值