PAT (Advanced Level) 1031 Hello World for U (20 分)【Python/C++】【模拟】

PAT (Advanced Level) 1031 Hello World for U (20 分)

C++里直接开了个答案数组用来存答案了。
注意空格的ASCII码是32,
‘0’的ASCII码是48,

Python里开了个头指针,尾指针直接模拟。

Python:

from math import *
import sys
import numpy as np
str_in = input()
length = len(str_in)
#b = [int(n) for n in str_in.split()]

n1 = 0
n2 = 0
n3 = 0

n2 = length // 3;
if int(length) % 3 != 0:
    n2 += length % 3
n1 = (length - n2) >> 1
n3 = n1
if n2 <= n1 or n2 < 3:
    n2 += 2
    n1 -= 1
    n3 -= 1

fpos = 0
epos = length - 1 

for i in range (1, n1 + 1 + 1):
    if i == n1 + 1:
        for j in range(1, n2 + 1):
            print(str_in[fpos], end = "")
            fpos += 1  
        continue
    for j in range(1, n2 + 1):
        if j == 1:
            print(str_in[fpos], end = "")
            fpos += 1
            continue
        if j == n2:
            print(str_in[epos], end = "")
            epos -= 1
            continue
        print(" ", end = "")
            
        
 
            
    print("")





C++:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<map>
#include<queue>
#include<vector>
using namespace std;
#define ll long long int
#define INF 0x3f3f3f3f
const int maxn = 1e5 + 10;
string str;
int ans[100][100];
int main() {
	getline(cin, str);
	int len = str.length();
	int n1, n2, n3;//(The n1's length) = (the problem's n1's length) - 1;
	//The value of n1 in the solution to the problem has been changed to n1 - 1;
	n2 = len / 3;
	if (len % 3 != 0) {
		n2 += len % 3;
	}
	n1 = n3 = (len - n2) >> 1;
	if (n2 < 3 || n2 <= n1) {
		n1--; n3--;
		n2 += 2;
	}
	for (int i = 0; i < 100; i++) {
		for (int j = 0; j < 100; j++) {
			ans[i][j] = 32 - '0';
		}
	}
	for (int i = 1; i <= n1; i++) {
		ans[i][1] = str[i - 1] - '0';
	}
	for (int i = 1; i <= n2; i++) {
		ans[n1 + 1][i] = str[i + n1 - 1] - '0';
	}
	for (int i = 1; i <= n3; i++) {
		ans[n1 - i + 1][n2] = str[i + n1 + n2 - 1] - '0';
	}
	for (int i = 1; i <= n1 + 1; i++) {
		for (int j = 1; j <= n2; j++) {
			char c = ans[i][j] + '0';
			printf("%c", c);
		}
		printf("\n");
	}
	return 0;
}
//_CRT_SECURE_NO_WARNINGS
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值