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