我们今天的任务就是编写一个函数,接收一个整数n,然后打印出回升三角形形状。
规则:
- 数字之间需要一个空格;
- 每行结束后不需要空格;
示例:
输入:5, 输出:
1 1 1 1 1
2 2 2 2
3 3 3
4 4
5
题目难度:简单
题目来源:Upturn Numeral Triangle
def solution(n: int) → str:
# pass
assert solution(7) == " 1 1 1 1 1 1 1\n 2 2 2 2 2 2\n 3 3 3 3 3\n 4 4 4 4\n 5 5 5\n 6 6\n 7"
assert solution(12) == " 1 1 1 1 1 1 1 1 1 1 1 1\n 2 2 2 2 2 2 2 2 2 2 2\n 3 3 3 3 3 3 3 3 3 3\n 4 4 4 4 4 4 4 4 4\n 5 5 5 5 5 5 5 5\n 6 6 6 6 6 6 6\n 7 7 7 7 7 7\n 8 8 8 8 8\n 9 9 9 9\n 0 0 0\n 1 1\n 2"
nobugs
2
def solution(n: int) -> str:
return ''.join([(' ' + str(i if i < 10 else i % 10)) * (n - i + 1) + '\n' for i in range(1, n + 1)])[:-1]
assert solution(7) == " 1 1 1 1 1 1 1\n 2 2 2 2 2 2\n 3 3 3 3 3\n 4 4 4 4\n 5 5 5\n 6 6\n 7"
assert solution(
12) == "1 1 1 1 1 1 1 1 1 1 1 1\n 2 2 2 2 2 2 2 2 2 2 2\n 3 3 3 3 3 3 3 3 3 3\n 4 4 4 4 4 4 4 4 4\n 5 5 5 5 5 5 5 " \
"5\n 6 6 6 6 6 6 6\n 7 7 7 7 7 7\n 8 8 8 8 8\n 9 9 9 9\n 0 0 0\n 1 1\n 2 "
1 个赞
def solution(n: int) -> str:
m = n
print_info = ''
info_list = []
for i in range(1, n+1):
print_info_list = []
for j in range(0, m):
print_info_list.append(str(i%10))
m -= 1
info_list.append(" " + " ".join(print_info_list))
test = "\n".join(info_list)
return test
assert solution(7) == " 1 1 1 1 1 1 1\n 2 2 2 2 2 2\n 3 3 3 3 3\n 4 4 4 4\n 5 5 5\n 6 6\n 7"
assert solution(12) == " 1 1 1 1 1 1 1 1 1 1 1 1\n 2 2 2 2 2 2 2 2 2 2 2\n 3 3 3 3 3 3 3 3 3 3\n 4 4 4 4 4 4 4 4 4\n 5 5 5 5 5 5 5 5\n 6 6 6 6 6 6 6\n 7 7 7 7 7 7\n 8 8 8 8 8\n 9 9 9 9\n 0 0 0\n 1 1\n 2"
def solution(n: int) -> str:
# 每排数字个数(n - i + 1)
# 判断当前数字大于10还是小于10 (i if i < 10 else i % 10))
# 前面有(i-1)个空格
list_num = [(i-1)*" "+(" " + str(i if i < 10 else i % 10)) * (n - i + 1) for i in range(1, n+1)]
str_now = "\n".join(list_num)
print(str_now)
return str_now
assert solution(7) == " 1 1 1 1 1 1 1\n 2 2 2 2 2 2\n 3 3 3 3 3\n 4 4 4 4\n 5 5 5\n 6 6\n 7"
assert solution(12) == " 1 1 1 1 1 1 1 1 1 1 1 1\n 2 2 2 2 2 2 2 2 2 2 2\n 3 3 3 3 3 3 3 3 3 3\n 4 4 4 4 4 4 4 4 4\n" \
" 5 5 5 5 5 5 5 5\n 6 6 6 6 6 6 6\n 7 7 7 7 7 7\n 8 8 8 8 8\n 9 9 9 9\n" \
" 0 0 0\n 1 1\n 2"
def solution(n: int) -> str:
data = []
for i in range(n):
data_line = []
for j in range(i, n):
data_line.append(str((i + 1) % 10))
# data.append(' ' * i + ' '.join(data_line))
data.append(' ' + ' '.join(data_line))
return '\n'.join(data)
assert solution(7) == " 1 1 1 1 1 1 1\n 2 2 2 2 2 2\n 3 3 3 3 3\n 4 4 4 4\n 5 5 5\n 6 6\n 7"
assert solution(12) == " 1 1 1 1 1 1 1 1 1 1 1 1\n 2 2 2 2 2 2 2 2 2 2 2\n 3 3 3 3 3 3 3 3 3 3\n 4 4 4 4 4 4 4 4 4\n 5 5 5 5 5 5 5 5\n 6 6 6 6 6 6 6\n 7 7 7 7 7 7\n 8 8 8 8 8\n 9 9 9 9\n 0 0 0\n 1 1\n 2"
public void solution(int i) {
for (int j = i; j > 0; j--) {
for (int k = 0; k < j; k++) {
System.out.print(i - j + 1);
System.out.print(" ");
}
System.out.println();
}
}
def solution(n: int) -> str:
str_n = ""
list_n = []
for i in range(n):
# list_n = []
for j in range(n - i):
if i + 1 < 10:
list_n.append(f" {i + 1}")
else:
list_n.append(f" {str(i + 1)[-1]}")
if i < n-1:
list_n.append("\n")
str_n = "".join(list_n)
return str_n
assert solution(7) == " 1 1 1 1 1 1 1\n 2 2 2 2 2 2\n 3 3 3 3 3\n 4 4 4 4\n 5 5 5\n 6 6\n 7"
assert solution(12) == " 1 1 1 1 1 1 1 1 1 1 1 1\n 2 2 2 2 2 2 2 2 2 2 2\n 3 3 3 3 3 3 3 3 3 3\n 4 4 4 4 4 4 4 4 4\n 5 5 5 5 5 5 5 5\n 6 6 6 6 6 6 6\n 7 7 7 7 7 7\n 8 8 8 8 8\n 9 9 9 9\n 0 0 0\n 1 1\n 2"
def triangle(n:int):
m=1
y=n
while m<=y:
print(' ' * (m - 1), end='')
for i in range(n):
print(m,end=' ')
n-=1
m+=1
print()
triangle(8)
zhonghc
(格度)
10
def solution(n: int) ->str:
result = ''
for i in range(1, n+1):
one_line_number = str(i % 10)
if i == n:
one_line_info = ' '.join([one_line_number] * n)
else:
one_line_info = ' '.join([one_line_number] * n) + '\n'
one_line_info = ' ' * i + one_line_info
result += one_line_info
n -= 1
return result
Huis
11
def solution(n: int)->str:
return "\n".join([(" " + str((i + 1) % 10)) * (n - i) for i in range(n)])
lekaixin
(ALe阿乐)
12
def solution(n: int) -> str:
list1=[]
for i in range(1,n+1):
if i>=10:
list1.append((' '+str(i)[-1::])*(n+1-i))
else:
list1.append((' '+str(i))*(n+1-i))
return '\n'.join(list1)
assert solution(7) == " 1 1 1 1 1 1 1\n 2 2 2 2 2 2\n 3 3 3 3 3\n 4 4 4 4\n 5 5 5\n 6 6\n 7"
assert solution(12) == " 1 1 1 1 1 1 1 1 1 1 1 1\n 2 2 2 2 2 2 2 2 2 2 2\n 3 3 3 3 3 3 3 3 3 3\n 4 4 4 4 4 4 4 4 4\n 5 5 5 5 5 5 5 5\n 6 6 6 6 6 6 6\n 7 7 7 7 7 7\n 8 8 8 8 8\n 9 9 9 9\n 0 0 0\n 1 1\n 2"