1027 打印沙漏
本题要求你写个程序把给定的符号打印成沙漏的形状。例如给定17个“*”,要求按下列格式打印
*****
***
*
***
*****
所谓“沙漏形状”,是指每行输出奇数个符号;各行符号中心对齐;相邻两行符号数差2;符号数先从大到小顺序递减到1,再从小到大顺序递增;首尾符号数相等。
给定任意N个符号,不一定能正好组成一个沙漏。要求打印出的沙漏能用掉尽可能多的符号。
输入格式:
输入在一行给出1个正整数N(≤1000)和一个符号,中间以空格分隔。
输出格式:
首先打印出由给定符号组成的最大的沙漏形状,最后在一行中输出剩下没用掉的符号数。
输入样例:
19 *
输出样例:
*****
***
*
***
*****
2
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
解答1:
N,symbol = input().split()
N = int(N) - 1
maxsym = 3
while (N - maxsym * 2) >= 0:
N = N - maxsym * 2
maxsym += 2
maxsym -= 2
for i in range(maxsym,0,-2):
print(f"{symbol * i:^{maxsym}}")
for i in range(3,maxsym+1,2):
print(f"{symbol * i:^{maxsym}}")
print(N)
没通过测试点0,3
注意居中输出格式控制中可以有变量
https://blog.csdn.net/qq_41767116/article/details/121739140
解答2:
因为不知道测试点,百度
https://www.it610.com/article/1280081812030242816.htm
得知本题并不想输出后面的空格
N,symbol = input().split()
N = int(N) - 1
maxsym = 3
while (N - maxsym * 2) >= 0:
N = N - maxsym * 2
maxsym += 2
maxsym -= 2
for i in range(maxsym,0,-2):
print((maxsym-i)//2*" ",end="")
print(symbol*i)
for i in range(3,maxsym+1,2):
print((maxsym - i) // 2 * " ", end="")
print(symbol * i)
print(N)
注意,int*符号。所以 (maxsym-i)//2 而不是 (maxsym-i)/2。
1031 Hello World for U
Given any string of N (≥5) characters, you are asked to form the characters into the shape of U
. For example, helloworld
can be printed as:
h d
e l
l r
lowo
That is, the characters must be printed in the original order, starting top-down from the left vertical line with n1 characters, then left to right along the bottom line with n2 characters, and finally bottom-up along the vertical line with n3 characters. And more, we would like U
to be as squared as possible -- that is, it must be satisfied that n1=n3=max { k | k≤n2 for all 3≤n2≤N } with n1+n2+n3−2=N.
Input Specification:
Each input file contains one test case. Each case contains one string with no less than 5 and no more than 80 characters in a line. The string contains no white space.
Output Specification:
For each test case, print the input string in the shape of U as specified in the description.
Sample Input:
helloworld!
Sample Output:
h !
e d
l l
lowor
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB
解答:
list_s = list(input())
N = len(list_s) + 2
n = N//3
n2 = N - 2 * n - 2
for i in range(n-1):
print(f"{list_s[i]}{' '*n2}{list_s[N-3-i]}")
print("".join(list_s[n-1:n+n2+1]))